1

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
marka.thore
  • 2,795
  • 2
  • 20
  • 35
  • This does not answer your question, but have you taken a look at [the swing tutorial example about auto-completion in a text area](http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html). It might contain some useful hints – Robin Jan 14 '12 at 16:59
  • I already looked, it doesn't help me because in sun's tutorial the autocompletion feature is realized in a complete different way (without another frame). – marka.thore Jan 14 '12 at 17:03
  • Sun's tutorial uses SwingUtilities.invokeLater(). That might also help here as it should help ensure that the Document has been completely updated before you do your processing. – camickr Jan 14 '12 at 17:17

1 Answers1

2

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

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks for 2,3,4. About 1, actually I'm using an undecorated and unfocusable `JFrame`, you think isn't a good choice? – marka.thore Jan 16 '12 at 11:33
  • not that wrong idea, put there undecorated JDialog with parent and then toFront works in all cases, btw there are lot of issue manage two JFrames, starting with Focus, toFront .... – mKorbel Jan 16 '12 at 11:45