JRadioButton


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SelectRadioButton{
JLabel label;
final JButton btn;
final ButtonGroup bg;
final JFrame frame;
public static void main(String[] args){
SelectRadioButton sr = new SelectRadioButton(1);
}
public SelectRadioButton(){}
public SelectRadioButton(int a){
frame = new JFrame("Radio button selection");
btn=new JButton("Show");

JRadioButton first = new JRadioButton("First");
JRadioButton second = new JRadioButton("Second");
JRadioButton third = new JRadioButton("Third");
JRadioButton fourth = new JRadioButton("Fofurth");
JRadioButton fifth = new JRadioButton("Fifth");
JPanel panel = new JPanel();

panel.add(btn);
panel.add(first);
panel.add(second);
panel.add(third);
panel.add(fourth);
panel.add(fifth);

bg = new ButtonGroup();
bg.add(first);
bg.add(second);
bg.add(third);
bg.add(fourth);
bg.add(fifth);

btn.addActionListener(new MyAction());
first.addActionListener(new MyAction());
second.addActionListener(new MyAction());
third.addActionListener(new MyAction());
fourth.addActionListener(new MyAction());
fifth.addActionListener(new MyAction());
label = new JLabel("https://playjava.wordpress.com");
frame.add(panel, BorderLayout.NORTH);
frame.add(label, BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public class MyAction extends SelectRadioButton implements ActionListener{
public void actionPerformed(ActionEvent e)
{
// if(e.getSource()==btn)
// {
// String msg = bg.getSelection().toString();
// JOptionPane.showMessageDialog(frame,msg,"DEMO",JOptionPane.INFORMATION_MESSAGE);
// }
// else{
label.setText(e.getActionCommand());
JOptionPane.showMessageDialog(null,"This is the " + e.getActionCommand() +" radio button.");}
// }
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s