Close JPopupMenu on Keystroke

Hey Guys. Im creating tool to autocomplete Text for the Processing IDE. The following class Is the popup-menu that pops up to present the different fill-in possibilities:

class Entry extends JPopupMenu implements KeyListener{

    private JEditTextArea parent;
    
    public Entry(String word, JEditTextArea _parent) {
        super();
        parent = _parent;
        parent.add(this);
        
        parent.addKeyListener(this);
        
        add(word);
    }
    
    public void show(int x, int y) {
        super.show(parent, x, y);
    }
    
    public void keyPressed(KeyEvent key) {
        setVisible(false);
        setEnabled(false);
    }
    
    public void keyReleased(KeyEvent key) {}

    public void keyTyped(KeyEvent key) {}
}

The class gets called by the Autocomplete class, which passes the Sting of the typed word and the editor.getTextArea() as an argument.

The problem is, that as soon as the popup opens it blocks the further input of new characters and you have to manually close it by pressing ESC. I tried to make it a KeyListener and make it close on a keystroke, but had no success so far. Is there a way I can make it close on an incoming keystroke?

1 Like

Question has been answered here: https://stackoverflow.com/questions/63979634/close-jpopupmenu-on-keystroke/63980679#63980679

2 Likes