0

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
San Lewy
  • 136
  • 1
  • 3
  • 13

2 Answers2

1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

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

Community
  • 1
  • 1
user489041
  • 27,916
  • 55
  • 135
  • 204
  • This doesn't "simulate" the user invoking the Enter key. Also, Swing was designed to be used with `Key Bindings`, not a KeyListener. – camickr Oct 21 '14 at 18:36
  • Yes, you are correct. After reading the other question a little bit more, I see that. – user489041 Oct 21 '14 at 18:37