radiobutton problem and classCastException problem
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
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
Joined: Dec 06, 2000
Posts: 148
posted
0
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");
- Vinod<br />-------<br />SCJP2
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
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");