Changing a components values by an event of another avoiding recurse calls

Hello

assume there are two Components (e.g. an JCheckBox and a JList) placed on a JPanel. The state of the one components shall depend on the state of the other (e.g. If the user selects one or more items in the List, the checkbox has to be selected automatically whereas if the user deselects the checkbox, all the listitems shall be automatically deselected.)

If I implement two listener for each of the component which call the other ones API, I get recursive calls. (e.g. if I call "JCheckBox.setSelected(boolean)" by the event-method of the JList-Listener, this method would fire an event...)

One solution would be to implement a model, which is used by both of the components, but this seems to be a little bit oversized for this simple task. Is there another pattern that I could use?

Thanks

Thorsten

[844 byte] By [sw1679a] at [2007-11-27 8:24:58]
# 1
You could control it with a boolean flag.-Puce
Pucea at 2007-7-12 20:14:13 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks Puce,

I don't clearly wrote, what I meant - sorry: here a simpler example - two checkBoxes that have to act simultaneously (if the user clicks the chkBox1, the GUI has to check chkBox2 and vice versa), so my idea was:

in the Listener of chkBox1 I implemented

chkBox2.setSelected( !chkBox1.isSelected() )

in the Listener of chkBox2 I implemented

chkBox1.setSelected( !chkBox2.isSelected() )

But this gets a never ending calling.

Where can a boolean flag help me?

Thorsten

sw1679a at 2007-7-12 20:14:13 > top of Java-index,Desktop,Core GUI APIs...
# 3

As I said: you could use a boolean flag! :-)

Eg. something like this:

public class MyPanel extends JPanel{

private boolean inUpdate = false;

...

SomeListener sl1 = new SomeListener(){

public void someMethod(...){

if (! inUpdate){

inUpdate = true;

// update code -> calls someMethod of sl2

inUpdate = false;

}

}

}

SomeListener sl2 = new SomeListener(){

public void someMethod(...){

if (! inUpdate){

inUpdate = true;

// update code -> calls someMethod of sl1

inUpdate = false;

}

}

}

...

// register the listeners to the components

}

Pucea at 2007-7-12 20:14:13 > top of Java-index,Desktop,Core GUI APIs...
# 4
If your update methods do more you might want to use a separate boolean flag for each component.-Puce
Pucea at 2007-7-12 20:14:13 > top of Java-index,Desktop,Core GUI APIs...
# 5
Puce, You opened my eyes, now I can see. Thanks a lot!Thorsten
sw1679a at 2007-7-12 20:14:13 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Thanks Puce,

> I don't clearly wrote, what I meant - sorry: here a

> simpler example - two checkBoxes that have to act

> simultaneously (if the user clicks the chkBox1, the

> GUI has to check chkBox2 and vice versa), so my idea

> was:

> in the Listener of chkBox1 I implemented

> chkBox2.setSelected( !chkBox1.isSelected() )

> in the Listener of chkBox2 I implemented

> chkBox1.setSelected( !chkBox2.isSelected() )

> But this gets a never ending calling.

> Where can a boolean flag help me?

> Thorsten

Umm .. isn't this classic radio button behavior?

dwga at 2007-7-12 20:14:13 > top of Java-index,Desktop,Core GUI APIs...
# 7

Now, that I read it again, I think you might be right! :-)

@OP:

Maybe also have a look at radio buttons or at least to ButtonGroup, which should work with any button (normally used with radio buttons, though):

http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#radiobutton

http://java.sun.com/javase/6/docs/api/javax/swing/ButtonModel.html

-Puce

Pucea at 2007-7-12 20:14:13 > top of Java-index,Desktop,Core GUI APIs...
# 8

Again - thanks a lot for your help! The second example is indeed exactly the "RadioButton". I won't implement your hint, Puce for that, don't worry. I just had no idea of a generally useable pattern for the kind of problem - not for this concrete example. Your hint was exactly the one I needed. I'll reward you a duke.

-Thorsten

sw1679a at 2007-7-12 20:14:13 > top of Java-index,Desktop,Core GUI APIs...