0

in my project, i have a empty combobox which i want to populate after clicking on it.

comboCurrent = new JComboBox<String>();     
comboCurrent.setBounds(265, 181, 80, 20);
add(comboCurrent);
comboCurrent.setEditable(true);
comboCurrent.setSelectedItem(null); 
comboCurrent.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // TODO populate here
        System.out.println(e);
    }
});

but somehow the action listener does not work here. is there a way to listen to the first click on the combobox while it is still empty?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
mowel87
  • 3
  • 2
  • `comboCurrent.setBounds(265, 181, 80, 20);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). (Perhaps you should give the combo a `Default Text`..) – Andrew Thompson Mar 24 '14 at 10:19

1 Answers1

2

ActionListener invokes only when you press Enter key. For first clicking I recommend you to use FocusListener or MouseListener on your JComboBox.

alex2410
  • 10,904
  • 3
  • 25
  • 41