Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can one make a JComboBox dropdown when it is given focus?

I find that a useful way to draw attention to a jcombobox when one wants the user to select from it is to make it drop down at the point it gains focus usually when the previous item has been completed by the user. How can this been done in Java?

like image 409
Tony Wolff Avatar asked Jan 16 '13 20:01

Tony Wolff


2 Answers

You could do:

comboBox.addFocusListener(new FocusAdapter() {

   @Override
   public void focusGained(FocusEvent e) {
      comboBox.showPopup();
   }
});
like image 62
Reimeus Avatar answered Oct 05 '22 12:10

Reimeus


You want JComboBox#setPopupVisible

Add in a FocusListener to monitor for focus gained and you should be right.

Depending on if the combo box is editable or not, you may need to add a focus listener to the editor as well

like image 41
MadProgrammer Avatar answered Oct 05 '22 10:10

MadProgrammer