双击 JList 元素后尝试将焦点集中到 JTextPane (Java)
问题:
我有以下 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 上,有什么想法吗?
如果这还不够清楚,我会详细说明。请帮忙,谢谢!
Problem:
I have the following JList which I add to the textPane, and show it upon the caret moving. However, after double clicking on the Jlist element, the text gets inserted, but the caret is not appearing on the JTextPane.
This is the following code:
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();
Any idea on how to "de-focus" the selection on the Jlist, or make the caret appear on the JTextPane after the text insertion?
I'll elaborate more if this is not clear enough. Please help, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看并使用 JComponent
特别是
grabFocus
和requestFocusInWindow
例如,如果您在
textPane 之后添加
?textPane.grabFocus()
会发生什么.moveCaretPosition(pos);Have a look and play around with the focus-methods in JComponent
Specifically
grabFocus
andrequestFocusInWindow
What happens for instance, if you add
textPane.grabFocus()
aftertextPane.moveCaretPosition(pos);
?虽然与您的问题无关,但您可能需要查看列表操作< /a>,它尝试以更通用的方式处理此类请求。
编辑:
这是我的简单 SSCCE,显示不需要 invokeLater:
Although not related to your problem, you may want to check out the List Action, which attempts to handle this type of request in a more general way.
Edit:
Here is my simple SSCCE that shows invokeLater is not required: