Java JRadioButton handle click event
// Demonstrate JRadioButton import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; class Demo extends JPanel implements ActionListener { JLabel jlab;//from w w w . ja v a 2 s. c o m Demo() { // Change to flow layout. setLayout(new FlowLayout()); // Create radio buttons and add them to content pane. JRadioButton b1 = new JRadioButton("A"); b1.addActionListener(this); add(b1); JRadioButton b2 = new JRadioButton("B"); b2.addActionListener(this); add(b2); JRadioButton b3 = new JRadioButton("C"); b3.addActionListener(this); add(b3); // Define a button group. ButtonGroup bg = new ButtonGroup(); bg.add(b1); bg.add(b2); bg.add(b3); // Create a label and add it to the content pane. jlab = new JLabel("Select One"); add(jlab); } // Handle button selection. public void actionPerformed(ActionEvent ae) { jlab.setText("You selected " + ae.getActionCommand()); } } public class Main { public static void main(String[] args) { Demo panel = new Demo(); JFrame application = new JFrame(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); application.add(panel); application.setSize(250, 250); application.setVisible(true); } }