-
May 22nd, 2012, 12:50 PM
#1
JComboBox get current value
I am trying to get the "Current" value from a combo box. With getSelected Value you actually have to select a value for it to trigger. I have a gui with several tabs. The tabs share a common combo box. When switching from one tab to another, I want to be able to grab the value in the combobox after the tab is swiched.
The default value of the combo box is "--Choose--". If I am on tab 1, and then switch to tab 2 and do a print of the combo box value, it comes up empty rather than "--Choose--".
Here is the code I am using to get the value in the combobox currently.
Code:
class GoActionListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
// musicListBox.remove(logoPanel);
String category = (String) categoryComboBox.getSelectedItem();
//String cat = (String)
System.out.println(category);
I want the value of Sys out to be "--Choose--" when the tab is switched.
-
May 22nd, 2012, 05:16 PM
#2
Re: JComboBox get current value
If I am on tab 1, and then switch to tab 2 and do a print of the combo box value, it comes up empty rather than "--Choose--".
It will unless you select an item.
You can call getSelectedIndex() and check to see if it returns -1 (nothing selected) and if so call getItemAt(0) to get the first item in the list.
-
May 23rd, 2012, 06:46 AM
#3
Re: JComboBox get current value
Yes, you are right, thanks!.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|