Jump to content
  • Advertisement
Sign in to follow this  
ju2wheels

[java] Editable JComboBox

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I have an editable JComboBox that I have added an Action and Key listener to to detect changes. My combo box starts off with* two choices of "" and "NULL" as selectable items. My problem is that I need to be able to get the text thats being displayed in the combo box selection window. If it was chosen from the menu of options I could just do getSelectedItem or getSelectedIndex, but if they type something into the box, I cant do that, therefore I cant compare it to "" to see if an appropriate entry was made. Any ideas on how I can get the text in the combo box? I dont want to have to keep track of every key that is type to determine if the box is empty or not. *edit

Share this post


Link to post
Share on other sites
Advertisement
SOLUTION:

JComboBox cmbBox = new JComboBox();
cmbBox.addItem("");
cmbBox.addItem("*");
cmbBox.addItem("NULL");

cmbBox.setEditable(true);

//Add the key listener to the editor, NOT the combo box itself
//this was the source of half of my troubles
cmbBox.getEditor().getEditorComponent().addKeyListener(listener);

//action listener added to combo box as normal
cmbBox.addActionListener(listener);

//getting the text in the box
cmbBox.getEditor().getItem();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!