Java Radio Button anomaly
I am writing a Java application, and I have four radio buttons, call them rb1, rb2, rb3, and rb4. I've added two of them to one radiobutton group, and the other two to a second radio buttongroup. Then I added all four of them to a panel. Distinct action listeners are defined for all four buttons.
However, when I click on the first button in one group it fires its own action listener and the one for the second button in the other group. That button that incorrectly fires won't fire at all when I click on it.
I can only suspect that they shouldn't all be in one panel, b开发者_JAVA技巧ut that seems a bit strained as an explanation. Any ideas?
Thanks in advance for any help.
John Doner
It is perfectly fine for several groups of radio buttons to live in the same panel.
Double check your listeners. Check that listener4 was actually added to rb4. You may actually have done this instead:
rb1.addActionListener(listener1);
rb2.addActionListener(listener2);
rb3.addActionListener(listener3);
rb1.addActionListener(listener4); // NOTE: listener added to rb1 instead of rb4
If this is what happened, then:
- Clicking on rb1 would fire listener1 and listener4
- Clicking on rb4 would do nothing (no listener attached to it)
Which seems to match your symptoms.
精彩评论