• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

radiobutton problem and classCastException problem

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem with JRadioButtons that I can't seem to figure out. I have 3 radiobuttons and a JButton. When I press the button I want it to tell me which of the radiobuttons was selected. Sounds easy enough but i can't get it working.
My code is:
public void actionPerformed(ActionEvent e)
{
JRadioButton rbutton = (JRadioButton)e.getSource();
Pay(rbutton);
} // end of actionperformed
public void Pay(JRadioButton rbutton)
{
if (rbutton == minamt)
JOptionPane.showMessageDialog(null, "min amount");
else
JOptionPane.showMessageDialog(null, "not");
} // end of pay
N.B minamt is the name of one of the radiobuttons.
When I run it I get a classCastException in javax.swing.JButton.
Have I made a silly mistake. Any help much appreciated.
Suhail Sarwar
 
Vinod Venugopal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JButton extends from AbstractButton whereas JRadioButton extends from JToggleButton(which extends from AbstractButton). So thats y u get a classcast exception.
You can try with this piece of code in ur actionListener
.
.
if(minamt.isSelected())
JOptionPane.showMessageDialog(null, "flowlayout");
else ....
JOptionPane.showMessageDialog(null, "otherlayout");
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the code works now.
Thanks

Originally posted by Vinod Venugopal:
JButton extends from AbstractButton whereas JRadioButton extends from JToggleButton(which extends from AbstractButton). So thats y u get a classcast exception.
You can try with this piece of code in ur actionListener
.
.
if(minamt.isSelected())
JOptionPane.showMessageDialog(null, "flowlayout");
else ....
JOptionPane.showMessageDialog(null, "otherlayout");

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic