hey guys! how can i capture the state of the radio button, I mean whether it is clicked or not. I am constructing it with the constructor. the code is as follows....... JRadioButton selection = JRadioButton("example",false); selection.addMouseListener(new GridListener()); public class GridListener extends MouseAdapter{ public void mousePressed(MouseEvent e){ _____________________
John Zukowski Author of <a href="http://www.amazon.com/exec/obidos/ASIN/189311578X/ref=ase_electricporkchop/107-7882751-0234939" target="_blank" rel="nofollow">"Definitive Guide to Swing for Java 2"</a>,<br /><a href="http://www.amazon.com/exec/obidos/ASIN/1893115925/ref%3Dase%5Felectricporkchop/102-5437230-7785719" target="_blank" rel="nofollow">"Java Collections"</a> and <a href="http://www.amazon.com/exec/obidos/ASIN/1893115984/ref%3Dase%5Felectricporkchop/102-5437230-7785719" target="_blank" rel="nofollow">"Learn Java with JBuilder 6"</a>
hey john! I want to do some things if it selected(true) and if it is made false I want to do some other things. I can get the source by e.getSource() but how can I know whether it is made true or not?. pls help me thanks in advance.....
If it's a radio button the event is fired when the button is clicked then the getSource() method will tell you which button was clicked. the other option is to use the getStateChange() which will tell you if it was selected or deselected. hope that helps Dave
the getStateChange() method is in the ItemEvent class. however if you only have one radio button can't you just use a JCheckBox? then register a listener on it like this: buttonName.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e){ if ( e.getStateChange() == ItemEvent.SELECTED ) //do something } }); now when the button is checked or unchecked it'll fire a ItemEvent and you can use the getStateChange() event to see if it was selected or unselected. Dave