Make an if statement generic

Hi,

I have to use if statement three times for three different jComboBoxes. I was wondering if there was a way I could achieve this so I do I have to repeat my if statement 3 times.

Could I use a loop? or since the if statement is small how could I write a generic if statement that would work with any combobox?

Here is my awesome code!! haha

int id;

if (jComboBoxDirectory.getSelectedIndex() == 0)

id= 1;

else if (jComboBoxDirectory.getSelectedIndex() == 1)

id = 2;

else if (jComboBoxDirectory.getSelectedIndex() == 2)

id = 3;

else

id = 4;

Thanks in advance!

[647 byte] By [mattv6603a] at [2007-10-3 3:29:30]
# 1
int getID(JComboBox jcb) { return jcb.getSelectedIndex() + 1;}
IanSchneidera at 2007-7-14 21:23:16 > top of Java-index,Java Essentials,Java Programming...
# 2
int aux=jComboBoxDirectory.getSelectedIndex();int id=(aux<3?aux+1:aux=4);
johnarevaloa at 2007-7-14 21:23:16 > top of Java-index,Java Essentials,Java Programming...
# 3
int index = jComboBoxDirectory.getSelectedIndex() ;int id = (0 <= index && index <=2) ? index + 1 : 4;
yawmarka at 2007-7-14 21:23:16 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks everyone.. That was a fast response!!
mattv6603a at 2007-7-14 21:23:16 > top of Java-index,Java Essentials,Java Programming...
# 5

> > int getID(JComboBox jcb) {

>return jcb.getSelectedIndex() + 1;

>

>

I've created this method but how would I call it? When I tried doing jComboBoxDirectory.getID I get an error that says It cannot find method getID?

Am i suppose to create a new object first?

Or I can just try one of these alternatives....

Message was edited by:

mattv6603

mattv6603a at 2007-7-14 21:23:16 > top of Java-index,Java Essentials,Java Programming...
# 6
getID(jComboBoxDirectory);getID(jComboBoxABC);getID(jComboBoxXYZ);
IanSchneidera at 2007-7-14 21:23:16 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks Again
mattv6603a at 2007-7-14 21:23:16 > top of Java-index,Java Essentials,Java Programming...