This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes getting text on JComboBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "getting text on JComboBox" Watch "getting text on JComboBox" New topic

getting text on JComboBox

Andy Woods

Joined: Nov 29, 2001
Posts: 5
To all,
I am having a bit of trouble getting text from a editable JComboBox field. I have checked the earlier posts on this topic and i still seem to have trouble with this.
When the user clicks on a button i would like to get the data stored in a combo box? I populate the fields with data from a selected file. Isnt there an easier way to get data from a box than handling an event?
JComboBox AOnePos = new JComboBox;
Thanks.. Andy

Sonny Pondrom
Ranch Hand

Joined: Jun 05, 2001
Posts: 128
I have used the selected item from a ComboBox when my ADD button is activated. But you could probably test if the selected item has changed. You would have to initialize it with a known item (like Null) and check if it changed. However I think an event is a better approach.
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
When your user clicks your button, you have to handle ActionPerformed for the button. You do not need to handle an event for the combo, simply read its text when you handle the button ActionPerformed :

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Since ur JCombobox is editable, the earlier given approaches may not work.
The best approach is
where c is ur JCombobox instance
Consider Paul's rocket mass heater.
subject: getting text on JComboBox