Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JComboBox Listeners 1

Status
Not open for further replies.

fatcodeguy

Programmer
Feb 25, 2002
281
CA
Hi, I have two JComboBoxes, but the selections in the second depends on what is selectedin the first. How do I set this up??

Thanks
 
You again ??!!! Buy "Java Swing" published by O'Reilly, thats where I learnt my Swing - its a great book with loads of adaptable code examples !

Right then - JComboBox listeners :

In your class somewhere, you initialise your ComboBox's :

///////
JComboBox myComboBox = new JComboBox();
JComboBox myComboBox2 = new JComboBox();
////////////
///and make this variable visible form anywhere in your code
int whatEverSelected = 0;
/////// then later in code, the actionevent listener method :

public void actionPerformed(ActionEvent e) {
Object comboChooser = ((JComboBox)e.getSource());
if (comboChooser == myComboBox) {
System.out.println(((String)((JComboBox)e.getSource()).getSelectedItem()));
whatEverSelected = 1; //register what was clicked so you can do stuff with it later with second combobox
//do some other stuff
}

if (comboChooser == myComboBox2 && whatEverSelected == 1) {
System.out.println(((String)((JComboBox)e.getSource()).getSelectedItem()));

//do some other stuff
whatEverSelected = 0; //reset this var
}
}

This will listen for clicks on the exact item selected in the first combobox, and also register with the variable 'whatEverSelected' what happened when the user clicked an item in the first. So when the user clicks an item in the second jcombobox, if a given item in the first was clicked earlier, then the go-ahead is OK to do whatever with the following code ....

I hope this makes sense, obviously you would have to find a less simple variable than 'whatEverSelected', but the idea remains the same, form a code-perspective.

I've had a couple of beers tonight, so if this makes no sense, then gives me a shout and I'll try to clarify !!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top