[java] Editable JComboBox

Started by
1 comment, last by ju2wheels 18 years, 8 months ago
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
Advertisement
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][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
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();

This topic is closed to new replies.

Advertisement