Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JCheckBox StackOverflow

2 views
Skip to first unread message

Justin

unread,
Feb 15, 2001, 7:47:02 PM2/15/01
to
I have a GUI program and I have two JCheckBox's. I want only one of the
box's to be selected at any one time so I added ItemListener to both box's
like so ( the program implemets ItemListener).

checkBox.addItemListener(this);
checkBox2.addItemListener(this);

and did the following to ensure that only one could be selected.

public void itemStateChanged(ItemEvent evt) {

try{

if (evt.getSource() == checkBox2) {

checkBox.setSelected(false);

}

if (evt.getSource() == checkBox){

checkBox2.setSelected(false);

}

}

catch (Exception e) {System.err.println("kkkkk " + e);}

}

This works if I do it just for one checkbox i.e. comment out the
checkbox2.setselected(false) statement but with both I get the following
execetion continually looping error:

Any help appreciated...

Justin..

Exception occurred during event dispatching:

java.lang.StackOverflowError

at java.lang.ref.Reference.<init>(Reference.java:198)

at java.lang.ref.Reference.<init>(Reference.java:195)

at java.lang.ref.WeakReference.<init>(WeakReference.java:41)

at java.util.WeakHashMap$WeakKey.<init>(WeakHashMap.java:109)

at java.util.WeakHashMap$WeakKey.create(WeakHashMap.java:115)

at java.util.WeakHashMap$WeakKey.access$0(WeakHashMap.java:113)

at java.util.WeakHashMap.get(WeakHashMap.java:250)

at
javax.swing.SystemEventQueueUtilities.queueComponentWorkRequest(SystemEventQ
ueueUtilities.java:219)

at javax.swing.RepaintManager.addDirtyRegion(RepaintManager.java:234)

at javax.swing.JComponent.repaint(JComponent.java:3410)

at java.awt.Component.repaint(Component.java:1872)

at
javax.swing.plaf.basic.BasicButtonListener.stateChanged(BasicButtonListener.
java:133)

at javax.swing.AbstractButton.fireStateChanged(AbstractButton.java:987)

at
javax.swing.AbstractButton$ButtonChangeListener.stateChanged(AbstractButton.
java:1030)

at
javax.swing.DefaultButtonModel.fireStateChanged(DefaultButtonModel.java:332)

at
javax.swing.JToggleButton$ToggleButtonModel.setSelected(JToggleButton.java:2
15)

at javax.swing.AbstractButton.setSelected(AbstractButton.java:197)

at User.itemStateChanged(User.java:241)

Ryan Meyer

unread,
Feb 15, 2001, 12:32:58 PM2/15/01
to
Justin,

The exception you're seeing is the result of an infinite loop. The
"itemStateChanged" callback occurs both when the item is
selected, AND when it is deselected. Try adding the statement

if ( evt.getStateChange().equals( ItemEvent.SELECTED )) {


}

around your try block.

Ryan

Thierry Hanot

unread,
Feb 16, 2001, 4:45:25 AM2/16/01
to
Justin wrote:

Try this
public void itemStateChanged(ItemEvent evt) {

try {
// you want to check only if both are selected
if (checkBox.isSelected() && checkBox2.isSelected()) {

if (evt.getSource() == checkBox) {

checkBox2.setSelected(false);

}
else if (evt.getSource() == checkBox2) {

checkBox.setSelected(false);

}
}

}

catch (Exception ex) {
System.err.println("kkkkk " + ex.toString());
}

Your problem:
you call a setSelected in an item change of the same JComponent :

user click
=> itemStateChange
=> setSelected
=> itemStateChange
=> setSelected
=> .........

Why you didn't use an ActionPerformed ???
Thierry


0 new messages