How can I get the input for an editable JComboBox. When user gives an input to the combo how I can get the input text from it?
Asked
Active
Viewed 2.3k times
2
Andrew Thompson
- 168,117
- 40
- 217
- 433
special
- 621
- 3
- 11
- 20
-
See actually i need to get input through Jcombo. I know how to deal with the Getselecteditem. but it doesn't work; (the input will be string;) – special Apr 09 '12 at 11:18
-
And looking further you may have found [`getEditor()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComboBox.html#getEditor%28%29`) – Howard Apr 09 '12 at 11:31
-
jComboBox2.getEditor().toString().toUpperCase(); Doesn't Work..! :( – special Apr 09 '12 at 11:37
-
@AndrewThompson +1 on the link of 'What have you tried?'! – Federico Vera Apr 09 '12 at 11:48
-
@Fido Newbies should book-mark it, and check it before asking a question. – Andrew Thompson Apr 09 '12 at 11:50
3 Answers
8
You need to get the edited text from the combobox editor via combo.getEditor().getItem().
Howard
- 38,639
- 9
- 64
- 83
4
If you need the text that is selected on a JComboBox and you are sure it's a String and not any other object, just use something like String text = (String)myCombobox.getSelectedItem().
If the thing you have in your Model is other than a String, then you need to cast it to the appropriate class, and then use the toString() method of that object.
If you need more help, you should paste a bit of your code, at least declaration and inicialization of your JComboBox...
Federico Vera
- 1,357
- 1
- 12
- 18
1
Just have a look at the oracle tutorial. They do explain how to handle the common swing components http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
zip
- 579
- 1
- 3
- 16