[java] Editable JComboBox
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
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.
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();
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
Popular Topics
Advertisement