问题:试图让聚焦到JTextPane中双击JList的元素(爪哇)后
我有以下的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?
如果不够清楚,我会详细说明。请帮忙,谢谢!
@aioobe:嗯。首先尝试它的工作。然后我再次双击列表,并在textPane上显示文本后,不再有插入符号。为什么这很奇怪? – 2010-05-03 18:55:19
我注意到,第二次双击后,焦点运行到'JToolBar'(其中一个按钮)。我该怎么办? – 2010-05-03 18:59:07
尝试在SwingUtilities.invokeLater中放置grabfocus。 – aioobe 2010-05-03 19:29:38