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?