954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

comparing a value to a value in a jComboBox

I have a ComboBox defined as follows:

private String[] months = {"Month", "01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12"};
private JComboBox jComboBoxMonth = null;

and down further I have:

jComboBoxMonth = new JComboBox(months);

Now i am trying to do run an if statement which checks to see if the word "Month" is what is appearing in the ComboBox. So down further I have:

if (jComboBoxMonth.equals("Month") {
and my code is here.

Is the above test in the if statement being carried out, as i suspect that it isn't.

Anyone any ideas.

Thanks
Paidi

pmhayden
Newbie Poster
15 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

No, you have to first call currently selected item
String selectedOption = (String) jComboBoxMonth.getSelectedItem()
and then you can compare it as you did it if (selectedOption.equals("Month")

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You