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?
1) I hope that (little window ) ==
JWindowdon’t forget declare JWindow(Window owner)2) then you can two choices use
JTablewith one Column, there so simple to implements RowFilter, no required solving anything that you describe, only implemented filtering from simpleJTextFieldJListwith AutoComplete JComboBox / JTextField3)
KeyListenerisn’t correct listener look atDocument/DocumentListener4) easiest and most confortable way is implement a
JTable, and with hightlighting