hello!
i need your help to know which checkbox cause the event:
i have 2 checkboxes and 2 radiobutton (in swing),then i add the 2 checkboxes to a buttongroup and the 2 radiobutton to a buttongroup,so as default 1 checkbox and 1 radiobutton are selected,so when i check other radiobutton or other checkbox i want to change a string value,so what i need that to know which checkbox and which radiobutton caused the event because i can again check the original checkbox(default one).
thank very much.

Dani AI

Generated

As suggested, the event object does contain the source. If you want to keep addItemListener(this) in the same class, the usual, reliable pattern is: keep your checkboxes/radiobuttons as instance fields, add this as the ItemListener, then in itemStateChanged cast e.getSource() and compare object identity to the fields. 's idea of distinct listeners or identifiers works too, and 's point about inner classes is a convenient alternative, but the single-listener approach below is simple and clear.

Example pattern (inside the same class that implements ItemListener):

private JCheckBox cb1 = new JCheckBox("CB1");
private JCheckBox cb2 = new JCheckBox("CB2");
private JRadioButton rb1 = new JRadioButton("R1");
private JRadioButton rb2 = new JRadioButton("R2");

public MyPanel() {
  cb1.addItemListener(this);
  cb2.addItemListener(this);
  rb1.addItemListener(this);
  rb2.addItemListener(this);
}

@Override
public void itemStateChanged(ItemEvent e) {
  AbstractButton src = (AbstractButton) e.getSource();
  if (e.getStateChange() == ItemEvent.SELECTED) {
    if (src == cb1) { /* set string for cb1 */ }
    else if (src == cb2) { /* set string for cb2 */ }
    else if (src == rb1) { /* set string for rb1 */ }
    else if (src == rb2) { /* set string for rb2 */ }
  }
}

Troubleshooting checklist if getSource() "doesn't work":

  • Make sure the components are fields (not local vars) so the handler compares the same instances.
  • Compare by identity (==) to the stored field (or use getActionCommand() you set yourself), not by recreating objects.
  • Ensure the class actually implements ItemListener and itemStateChanged(ItemEvent e) has the correct signature.
  • Note that if you put JCheckBox instances into a ButtonGroup they'll behave mutually exclusive (like radio buttons); use JRadioButton if you intended exclusivity. If you prefer to know the current selection from a ButtonGroup instead of handling SELECTED events, iterate buttonGroup.getElements() and match getModel() to buttonGroup.getSelection().

Recommended Answers

All 4 Replies

Uhm, the getSource() method of the Event?

If you want two checkboxes to do different things, why dont you give them different actionListeners?

Or, you can pass an identifier in the constructor of the ActionListener, say an integer, and switch through these so:

checkboxOne.addActionListener(1);
checkboxOne.addActionListener(2);

//and inside the actionListener:

switch(thatThingICalledItInTheConstructor)
{
case 1:
   //do action for checkbox one
   break;
case 2:
   //dp action for checkbox two!
   break;
}

If you give it the same actionListener, that does the same action. Thats probably because you want the two check boxes to do the same thing...

thank you for your reply.
but getSource don't work.
and i want not in other class that i define the listener just i want in same class =>
addItemListener(this);
thank you!

Then I guess you are out of luck.

Seriously, you haven't said why getSource() won't work (it will by the way) and you haven't given any good reason not to define separate listeners. You can define separate listeners as inner classes, anonymous or named.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.