SWING - Radio Buttons

java.lang.Object
extended byjava.awt.Component
extended byjava.awt.Container
extended byjavax.swing.JComponent
extended byjavax.swing.AbstractButton
extended byjavax.swing.JToggleButton
extended byjavax.swing.JRadioButton

Constructor Summary
JRadioButton()
Creates an initially unselected radio button with no set text.
JRadioButton(Action a)
Creates a radiobutton where properties are taken from the Action supplied.
JRadioButton(Icon icon)
Creates an initially unselected radio button with the specified image but no text.
JRadioButton(Icon icon, boolean selected)
Creates a radio button with the specified image and selection state, but no text.
JRadioButton(String text)
Creates an unselected radio button with the specified text.
JRadioButton(String text, boolean selected)
Creates a radio button with the specified text and selection state.
JRadioButton(String text, Icon icon)
Creates a radio button that has the specified text and image, and that is initially unselected.
JRadioButton(String text, Icon icon, boolean selected)
Creates a radio button that has the specified text, image, and selection state.

Method Summary
protected void configurePropertiesFromAction(Action a)
Factory method which sets the ActionEvent source's properties according to values from the Action instance.
protected PropertyChangeListener createActionPropertyChangeListener(Action a)
Factory method which creates the PropertyChangeListener used to update the ActionEvent source as properties change on its Action instance.
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JRadioButton.
String getUIClassID()
Returns the name of the L&F class that renders this component.
protected String paramString()
Returns a string representation of this JRadioButton.
void updateUI()
Resets the UI property to a value from the current look and feel.

Example

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

public class Radiobutton extends JApplet implements ItemListener{
JTextField text;
JRadioButton radio1, radio2, radio3, radio4;
ButtonGroup group;

public void init() {

Container frame1=getContentPane();
frame1.setLayout(new FlowLayout());

group=new ButtonGroup();

radio1=new JRadioButton("Radio Button 1");
radio2=new JRadioButton("Radio Button 2");
radio3=new JRadioButton("Radio Button 3");
radio4=new JRadioButton("Radio Button 4");
text=new JTextField(20);

group.add(radio1);
group.add(radio2);
group.add(radio3);
group.add(radio4);

radio1.addItemListener(this);
radio2.addItemListener(this);
radio3.addItemListener(this);
radio4.addItemListener(this);

frame1.add(radio1);
frame1.add(radio2);
frame1.add(radio3);
frame1.add(radio4);

frame1.add(text);
}

public void itemStateChanged(ItemEvent e){
if(e.getItemSelectable()==radio1){
text.setText("You Select Radio Button 1");
}else if(e.getItemSelectable()==radio2){
text.setText("You Select Radio Button 2");
}else if(e.getItemSelectable()==radio3){
text.setText("You Select Radio Button 3");
}else if(e.getItemSelectable()==radio4){
text.setText("You Select Radio Button 4");
}


}
}

Output