Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Editable JComboBox

Started by ju2wheels Aug 9, 2005 at 8:18 AM 1 replies 1.2k views
Original Post
ju2wheels
ju2wheels
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
CaptainJester
CaptainJester
You might have to use a ComboBoxModel to do it. I haven't looked into this yet, but that is how you do it with JList and JTable.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
ju2wheels
ju2wheels
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();

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.