什么时候调用 getMagicCaretPosition() 是合法的
我正在开发一个自动完成功能(包含在插入符号位置下的一个小窗口中,建议您将要编写的内容)扩展一个 JTextPane
,并在一个关联到的 KeyAdapter
中这个组件我是这样写的:
...
@Override
public void keyReleased(KeyEvent e) {
if (Character.isLetterOrDigit(e.getKeyChar())) {
p = getCaret().getMagicCaretPosition();
if (p != null) {
SwingUtilities.convertPointToScreen(p, this);
p.x = p.x + 2;
p.y = p.y + 20;
autocompletion.setLocation(p.x, p.y);
}
}
}
...
问题是有时getCaret().getMagicCaretPosition()
返回null,我不明白为什么。
调用此方法或实现此功能的合法方式是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)我希望(小窗口)==
JWindow
不要忘记声明JWindow(窗口所有者)2) 那么你可以有两种选择使用
JTable
和一列,实现起来很简单 <一个href="http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting" rel="nofollow noreferrer">RowFilter,不需要解决您描述的任何问题,只需实现 使用一个列从简单的JTextField
3)
KeyListener
不是正确的侦听器看看Document/DocumentListener
4)最简单、最舒适的方法是实现
JTable
,并使用 突出显示1) I hope that (little window ) ==
JWindow
don't forget declare JWindow(Window owner)2) then you can two choices use
JTable
with one Column, there so simple to implements RowFilter, no required solving anything that you describe, only implemented filtering from simpleJTextField
JList
with AutoComplete JComboBox / JTextField3)
KeyListener
isn't correct listener look atDocument/DocumentListener
4) easiest and most confortable way is implement a
JTable
, and with hightlighting