• Post Reply Bookmark Topic Watch Topic
  • New Topic

urgent!!!!!!!

 
Surya Renduchintala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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){
_____________________



}
}
Thanks in advance..........
 
John Zukowski
Author
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ask the source of the event.
------------------
John Zukowski Author of "Definitive Guide to Swing for Java 2" and "Java Collections"
 
Surya Renduchintala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Surya Renduchintala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
guys!
i have only one radio button and as u suggested dave , i dont find any getStateChange in JRadioButton class. can some pls help!!!
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops, my last sentence should say the getStateChange() method not event. Don't want to confuse anyone, sorry 'bout that.


Dave
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!