Two JComboBoxes One change to other

R

RC

I have two JComboBoxes

JComboBox combo1 = new JComboBox();
JComboBox combo2 = new JComboBox();

Both I implemented the ItemListener
Now I want if combo1 itemStateChanged(ItemEvent evet)
then combo2 also itemStateChanged.

Vice versa, if combo2 itemStateChanged, then combo1 also
itemStateChanged.

Any idea how to do that?

Thank Q in advance!
 
D

derek

I have two JComboBoxes
JComboBox combo1 = new JComboBox();
JComboBox combo2 = new JComboBox();
Both I implemented the ItemListener
Now I want if combo1 itemStateChanged(ItemEvent evet)
then combo2 also itemStateChanged.
Vice versa, if combo2 itemStateChanged, then combo1 also
itemStateChanged.
Any idea how to do that?
Thank Q in advance!

Do you want both of the comboboxes to have the same data selected?
If so then just set the selection in the other combobox in your itemlistener class.

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComboBox.html#setSelectedIndex(int)

Just be aware that if you are listening in both comboboxes then as soon as you set the other box it will trigger its itemlistener.
Which would send you to a nice endless loop.
You would either have to use some sort of flag or remove the listeners when you set it manually.

..
=====================================================
THIS IS MY SIGNATURE. There are many like it, but this one is mine.
 
B

brahma.mca07

I have two JComboBoxes

JComboBox combo1 = new JComboBox();
JComboBox combo2 = new JComboBox();

Both I implemented the ItemListener
Now I want if combo1 itemStateChanged(ItemEvent evet)
then combo2 also itemStateChanged.

Vice versa, if combo2 itemStateChanged, then combo1 also
itemStateChanged.

Any idea how to do that?

Thank Q in advance!

hi
your question is quite intresting.............you want to use to
associated events & both are inter related ........................so
you must serialize them.........i thing its better to use boolean
variable to serialize events ......................
thanks
 
R

Roedy Green

Vice versa, if combo2 itemStateChanged, then combo1 also
itemStateChanged.

Any idea how to do that?

Use a listener that sets the state of the other. the problem is you
get fibrillation, where each change triggers another, ad-infinitum.

So you must set a boolean somewhere to say, "ignore trigger"

When you get a trigger you check the boolean. If it says ignore, you
set it false and do nothing. If it is false you set it true and
trigger the other widget.
 
D

Daniele Futtorovic

Use a listener that sets the state of the other. the problem is you
get fibrillation, where each change triggers another, ad-infinitum.

So you must set a boolean somewhere to say, "ignore trigger"

When you get a trigger you check the boolean. If it says ignore, you
set it false and do nothing. If it is false you set it true and
trigger the other widget.

Depends on whether the relationship between his two models is bijective,
though, that is whether any item of any of the two models is mapped to
exactly one of the other model. If it is, it should work alright, since
calling setSelectedItem with the currently selected item (may be null)
doesn't fire any ItemEvent (though it does fire an ActionEvent).

Follow-up to c.l.j.g
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top