San Lewy
San Lewy

Reputation: 136

Is there a postActionEvent for a JComboBox?

I successfully use postActionEvent() on a JTextField that has an ActionListener to simulate User action (pressing Enter key). I would like to create the same type of simulation for a JComboBox that has an ActionListener, but I don't find a postActionEvent() for a JComboBox. How could this (simulating User pressing Enter key) be accomplished?

Upvotes: 0

Views: 210

Answers (2)

user489041
user489041

Reputation: 28304

You could also use a KeyListener:

addKeyListener(new KeyAdapter() {

    @Override
    public void keyReleased(KeyEvent event) {
        if (event.getKeyChar() == KeyEvent.VK_ENTER) {
            if (((JTextComponent) ((JComboBox) ((Component) event
                    .getSource()).getParent()).getEditor()
                    .getEditorComponent()).getText().isEmpty())
                System.out.println("please dont make me blank");
        }
    }
});

See this question

Upvotes: 0

camickr
camickr

Reputation: 324118

How could this (simulating User pressing Enter key) be accomplished?

Combobox has an "enterPressed" Action. So you should be able to access the Action from the ActionMap of the combobox and then manually invoke the actionPerformed(...) method of the Action.

Check out Key Bindings for a program to list all the bindings for all Swing components.

Upvotes: 1

Related Questions