
I have created a web page with radio button group with two radio buttons for accepting the home appliances categories,Kitchen appliances and other appliances.I want to check for the appliance chosen when the required radio button is selected.Which event listener do I need to implement for this task.

Java Swing action on JRadioButtons
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class RadioButtons extends JFrame {
private JRadioButton b1, b2, b3;
public RadioButtons() {
setLayout(null);
ButtonGroup group = new ButtonGroup();
b1 = new JRadioButton("home appliances");
b2 = new JRadioButton("kitchen appliances");
b3 = new JRadioButton("other appliances");
group.add(b1);
group.add(b2);
group.add(b3);
b1.setBounds(10,10,100,20);
b2.setBounds(10,40,100,20);
b3.setBounds(10,70,100,20);
add(b1);
add(b2);
add(b3);
b1.addActionListener(new MyAction());
b2.addActionListener(new MyAction());
b3.addActionListener(new MyAction());
setSize(300,150);
setVisible(true);
}
public static void main(String[] args) {
new RadioButtons();
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"You have selected " + e.getActionCommand());
}
}
}
For more information, visit the link JRadioButtons Example
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.