Saturday, 15 March 2014

java - How to get value that has been written in editable JComboBox? -


I keep searching and it seems that everyone is just using JComboBox #getSelectedItem is. But my combo box is editable and the user can enter anything getSelectedItem method returns one of the actual objects in the combo box, the field Not recorded in the string

image description

if in my box "bar" and "Item" and the user enters "foo", so I have to get "foo"!

Why getSelectedItem does not work

It was indicated that getSelectedItem returns the entered string as well. However, it was not indicated that it only works when the user prevents the field from being edited. I attached these event listeners:

  component [] comps = input.getComponents (); // The third text field component is compose [2] .addKeyListener (new keyListener) {public zero key type (keyEvent e) {doSomething ();}}); // Even after leaving user input field fire incident AddActionListener (new overlapping public hyperactive ActionPerfect (Action Event E) {doSomething ();}});  

And the results were:

  KeyEvent:. JComboBox.getEditor () GetItem () = 6 JComboBox.getSelectedItem () = Null KeyEvent: JComboBox. .getEditor () GetItem () = 66 JComboBox.getSelectedItem () = Null KeyEvent:. JComboBox.getEditor () GetItem () = 666 JComboBox.getSelectedItem () = Nulled ActionEvent:. JComboBox.getEditor () GetItem () = 6666 JComboBox.getSelectedItem () = 6666  

As you can see, action event listener can capture value, but not main event Could

like this: combobox.getEditor (). GetItem () . Good drawing


No comments:

Post a Comment