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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jcombobox selecting item problem

Status
Not open for further replies.

JJJacq

Programmer
May 31, 2005
9
CA
Hi, this has probably been asked before but I can't find any reference to it.In this sample of code I am trying to set the index of a combo box from an action_event triggered in another combo box.
I am having problems using the selectedIndex() method, sometimes it does not work like in this code:

void jComboBoxFitSel_itemStateChanged(ItemEvent e) {

if (jComboBoxFitSel.getSelectedIndex() == 4)
{
jLabel6.setText("pass 4");
jComboDia.removeAllItems();
jComboDia.insertItemAt("2.0",0);
jComboDia.insertItemAt("2.5",1);
jComboDia.insertItemAt("3.0",2);
jComboDia.insertItemAt("4.0",3);
jComboDia.insertItemAt("6.0",4);
jComboDia.insertItemAt("8.0",5);
jComboDia.insertItemAt("10.0",6);
jComboDia.insertItemAt("12.0",7);
jComboDia.insertItemAt("14.0",8);
jComboDia.insertItemAt("16.0",9);
jComboDia.insertItemAt("18.0",10);
jComboDia.insertItemAt("20.0",11);
jComboDia.insertItemAt("24.0",12);
jComboDia.setSelectedIndex(0);
}
else
{
jLabel6.setText("pass not 4");
jComboDia.removeAllItems();
jComboDia.insertItemAt("0.5",0);
jComboDia.insertItemAt("0.75",1);
jComboDia.insertItemAt("1.0",2);
jComboDia.insertItemAt("1.25",3);
jComboDia.insertItemAt("1.5",4);
jComboDia.insertItemAt("2.0",5);
jComboDia.insertItemAt("2.5",6);
jComboDia.insertItemAt("3.0",7);
jComboDia.insertItemAt("4.0",8);
jComboDia.insertItemAt("6.0",9);
jComboDia.insertItemAt("8.0",10);
jComboDia.insertItemAt("10.0",11);
jComboDia.insertItemAt("12.0",12);
jComboDia.insertItemAt("14.0",13);
jComboDia.insertItemAt("16.0",14);
jComboDia.insertItemAt("18.0",15);
jComboDia.insertItemAt("20.0",16);
jComboDia.insertItemAt("24.0",17);
jComboDia.setSelectedIndex(3);
}

}
}
when either one of these conditons are filled the jComboDia.setSelectedIndex command does not execute, is there some problem with trying to set an index in a combo box from another combo box.

Cheers, thanks for any help.

Jack
 
When you say 'does not execute', do you know that for a fact (you've used the debugger, for instance, so know that the code was never executed), or are you making an assumption because nothing seems to happen 'on-screen' to your combo?

Another point maybe to be aware of... if you have an action listener on your jComboDia, then I think that invoking setSelectedIndex() on it will fire the listener.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Hi Tim, I know that the code executes but he jComboDia box should have an item selected during run time. It seems no item is selcted and the JComboDia box is blank. The jComboDia box does not have an actionListerner.

Cheers,

Jack
 
Okay, two things then.

The itemStateChanged event gets fired more than once when you change the combo selection. You should call the getStateChange() method of the ItemEvent passed in to determine what is happening.

The new selection is not really established on the JComboBox it this stage. You need to get your info from the ItemEvent. There is a getItem() method on it which informs you of which item entry the event is dealing with. So if getStateChange() returns an ItemEvent.DESELECTED value, the getItem() will tell you which one the combo was on previous to the user's new selection. If it returns an ItemEvent.SELECTED value, it tells you which item is about to be selected.

You should probably process BOTH values and only change the list in the second combo if it is warranted, i.e. from the item at the 4th position, or to it.

Something like :-
Code:
public void itemStateChanged(ItemEvent e) {
  if ( e.getStateChange() == ItemEvent.SELECTED && e.getItem().equals([i]<your 4th item>[/i]) ) {
          
    jComboDia.removeAllItems();
    jComboDia.insertItemAt("2.0",0);
    jComboDia.insertItemAt("2.5",1);
    jComboDia.insertItemAt("3.0",2);
    jComboDia.insertItemAt("4.0",3);
    jComboDia.insertItemAt("6.0",4);
    jComboDia.insertItemAt("8.0",5);
    jComboDia.insertItemAt("10.0",6);
    jComboDia.insertItemAt("12.0",7);
    jComboDia.insertItemAt("14.0",8);
    jComboDia.insertItemAt("16.0",9);
    jComboDia.insertItemAt("18.0",10);
    jComboDia.insertItemAt("20.0",11);
    jComboDia.insertItemAt("24.0",12);
    jComboDia.setSelectedIndex(0);

  } else if ( e.getStateChange() == ItemEvent.DESELECTED && e.getItem().equals([i]<your 4th item>[/i]){
    jComboDia.removeAllItems();
    jComboDia.insertItemAt("0.5",0);
    jComboDia.insertItemAt("0.75",1);
    jComboDia.insertItemAt("1.0",2);
    jComboDia.insertItemAt("1.25",3);
    jComboDia.insertItemAt("1.5",4);
    jComboDia.insertItemAt("2.0",5);
    jComboDia.insertItemAt("2.5",6);
    jComboDia.insertItemAt("3.0",7);
    jComboDia.insertItemAt("4.0",8);
    jComboDia.insertItemAt("6.0",9);
    jComboDia.insertItemAt("8.0",10);
    jComboDia.insertItemAt("10.0",11);
    jComboDia.insertItemAt("12.0",12);
    jComboDia.insertItemAt("14.0",13);
    jComboDia.insertItemAt("16.0",14);
    jComboDia.insertItemAt("18.0",15);
    jComboDia.insertItemAt("20.0",16);
    jComboDia.insertItemAt("24.0",17);
    jComboDia.setSelectedIndex(3);}

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Tim, thanks very much, that works. It looks like listening and acting on events is a bit more complicated than I thought.

Cheers,

Jacques
 
woops, spoke too soon, the first condition works but not the second, it seems that I have to be very specific on the second condition such as:

else if (e.getStateChange() == ItemEvent.SELECTED && e.getItem().equals(jComboBoxFitSel.getItemAt(5)))
however I have many other items to which this condition also applies (item 0,1,2,3). So do I need to build up a complicated OR condition for all of these.

Jack
 
woops again, my mistake, it works fine using exactly your code. Thanks.
 
Incidentally, if you want to add items to a combo and you're adding in the same order they want to appear, then you can simply use the JComboBox.addItem(Object o) method (and it's slightly less typing).

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Tim, thanks for all your advice. I am still having some problems with the code. Everything depends on whetther item 4 was selected or deselected. The problem is it is possible that item 0 is selected and then item 1 selected which produces an error.


 
Any chance of posting the error?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Huuum, can't seem to duplicate the error at this point. One thing that is happening is that I cannot get through the first condition if the first item selected in jComoboxFitSel is 0, 1 or 2 anything but 4. This is pretty obvious since the condition is only looking at item 4. The trick is how to get the same behavior whether item 0, 1, 2, 3, 5 is selected and have something different happen when item 4 is selected. I tried using if (e.getStateChange() == ItemEvent.SELECTED && e.getItem().equals(jComboBoxFitSel.getItemAt(4))) and e.getStateChange() == ItemEvent.DESELECTED in the else if condition but this does not work.

Jack
 
Assuming the second combo is pre-populated with appropriate items, the conditions in the event handler are supposed to apply only when selection moves from, or to, item 4. If moving from item 1 to 3, say, then nothing needs to change in the second combo since it already holds the appropriate items. This is how I assumed you wanted it to work.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Yes, that's true the combo item in the 2nd combo do not need to change but I have added some calculations that are required when going from items 1 to 3.

if (e.getStateChange() == ItemEvent.SELECTED && e.getItem().equals(jComboBoxFitSel.getItemAt(4)))
{
//jLabel8.setText("pass 4");
buttSelect = true;
jComboDia.removeAllItems();
jComboDia.insertItemAt("2.0",0);
jComboDia.insertItemAt("2.5",1);
...
jComboDia.setSelectedIndex(0);
calc_Kfactor();
}
else if (e.getStateChange() == ItemEvent.DESELECTED && e.getItem().equals(jComboBoxFitSel.getItemAt(4)))
{
//jLabel8.setText("not pass 4");
buttSelect = false;
jComboDia.removeAllItems();
jComboDia.insertItemAt("0.5",0);
jComboDia.insertItemAt("0.75",1);
...
jComboDia.insertItemAt("24.0",17);
jComboDia.setSelectedIndex(4);
calc_Kfactor();
}

} //end void
 
OMG ... this is starting to get messy!

Maybe you could achieve a cleaner approach if you used a ComboBoxModel to encapsulate this logic? My first thought is that you either implement your own ComboBoxModel, or sublcass DefaultComboBoxModel, to act as the model for the second combobox. You'd give it a method of your own ... something like
Code:
MyComboModel.updateContents(Object triggerItem);

The idea is that the event handler simply checks for a 'SELECTED' event, and passes the item to your model on the second combo via the above method. The model should keep an internal state of what 'mode' of item data it currently displays and compare this state with the incoming 'triggerItem'. Based on any kind of logic you like (and this would be based on what item the user selected from combo1, NOT its index!!), the model would either ignore it or update its state and contents accordingly (remembering to fire the correct update event to allow combo2 to redraw itself).

This should protect your GUI class from getting bogged down with scary code, keeping the logic encapsulated in a separate model class.

Does this seem useful to you?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Hi Tim, yes this sounds like a good approach. I have never done a subclass before , is there an example somewhere?

Cheers,

Jack
 
It would be worth your while learning more about Java programming. Try this free ebook




Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top