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.
Re: JComboBox get current value
Quote:
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.
Re: JComboBox get current value
Yes, you are right, thanks!.