CONSTRUCTORS OF JRADIO BUTTON IN JAVA:
JRadioButton(String)
Here string specifies the text that have to appear with jradio button.
JRadioButton(Icon)
This constructor is used to set an image on jradio button.
JRadioButton(String,Icon)
When we want to create jradio button with image with text then this constructor is applicable.
JRadioButton()
This constructor creates simple jradio button with no text or image.
WHY WE USE BUTTON GROUP:
Radio button should be configure in button group because if a user press a radio button in that group only one button is selected at time if user select other first one will automatically deselected.
CODING:
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
class tab extends JFrame
implements ActionListener {
JTextField jtf;
tab()
{
super();
setTitle("Radio Button Example");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
//Adding a radio Button
JRadioButton r =new JRadioButton("A");
r.addActionListener(this);
JRadioButton r1 =new JRadioButton("B");
r1.addActionListener(this);
JRadioButton r2 =new JRadioButton("C");
r2.addActionListener(this);
//Creating Textfield
jtf=new JTextField(15);
FlowLayout f=new FlowLayout();
setLayout(f);
add(r);
add(r1);
add(r2);
add(jtf);
//Define a button group
ButtonGroup bg =new ButtonGroup();
bg.add(r);
bg.add(r1);
bg.add(r2);
setSize(250, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
jtf.setText(ae.getActionCommand());
}
}
class rad
{
public static void main(String args[])
{
tab x= new tab();
}
}
0 comments:
Post a Comment