問題:
次の JList を textPane に追加し、キャレットの移動時に表示します。ただし、Jlist 要素をダブルクリックすると、テキストが挿入されますが、JTextPane にキャレットが表示されません。
これは次のコードです。
listForSuggestion = new JList(str.toArray());
listForSuggestion.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listForSuggestion.setSelectedIndex(0);
listForSuggestion.setVisibleRowCount(visibleRowCount);
listScrollPane = new JScrollPane(listForSuggestion);
MouseListener mouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
JList theList = (JList) mouseEvent.getSource();
if (mouseEvent.getClickCount() == 2) {
int index = theList.locationToIndex(mouseEvent.getPoint());
if (index >= 0) {
Object o = theList.getModel().getElementAt(index);
//System.out.println("Double-clicked on: " + o.toString());
//Set the double clicked text to appear on textPane
String completion = o.toString();
int num= textPane.getCaretPosition();
textPane.select(num, num);
textPane.replaceSelection(completion);
textPane.setCaretPosition(num + completion.length());
int pos = textPane.getSelectionEnd();
textPane.select(pos, pos);
textPane.replaceSelection("");
textPane.setCaretPosition(pos);
textPane.moveCaretPosition(pos);
}
}
theList.clearSelection();
Jlist で選択を「焦点を外す」方法、またはテキスト挿入後にキャレットを JTextPane に表示する方法についてのアイデアはありますか?
これが十分に明確でない場合は、さらに詳しく説明します。助けてください、ありがとう!