• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

radiobutton problem and classCastException problem

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic