什么时候调用 getMagicCaretPosition() 是合法的

发布于 2024-12-26 19:34:05 字数 618 浏览 4 评论 0 原文

我正在开发一个自动完成功能(包含在插入符号位置下的一个小窗口中,建议您将要编写的内容)扩展一个 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,我不明白为什么。

调用此方法或实现此功能的合法方式是什么?

I'm developing an autocompletion functionality (that consists in a little window under the caret position that suggest what you'll writing) extending a JTextPane, and in a KeyAdapter associated to this component I wrote this:

...
@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);
        }
    }
}
...

The problem is that sometime getCaret().getMagicCaretPosition() returns null, and I can't understand why.

What is the legal way to invoke this method, or to implement this functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂牵梦绕锁你心扉 2025-01-02 19:34:05

1)我希望(小窗口)== JWindow不要忘记声明JWindow(窗口所有者)

2) 那么你可以有两种选择使用

  • JTable 和一列,实现起来很简单 <一个href="http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting" rel="nofollow noreferrer">RowFilter,不需要解决您描述的任何问题,只需实现 使用一个列从简单的 JTextField

  • 使用 rel="nofollow noreferrer">自动完成 JComboBox / 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

3) KeyListener isn't correct listener look at Document/DocumentListener

4) easiest and most confortable way is implement a JTable, and with hightlighting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文