Creating JRadioButton Components : JRadioButton « Swing « Java Tutorial






  1. A JRadioButton represents a radio button.
  2. You can use multiple JRadioButtons to represents a selection from which only one item can be selected.
  3. To indicate a logical grouping of radio buttons, you use a javax.swing.ButtonGroup object.
  4. To programmatically select a JRadioButton, you pass true to its setSelected method.
public JRadioButton()
JRadioButton aRadioButton = new JRadioButton();

public JRadioButton(Icon icon)
JRadioButton aRadioButton = new JRadioButton(new DiamondIcon(Color.CYAN, false));
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));

public JRadioButton(Icon icon, boolean selected)
JRadioButton aRadioButton = new JRadioButton(new DiamondIcon(Color.CYAN, false),  true);
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));

public JRadioButton(String text)
JRadioButton aRadioButton = new JRadioButton("4 slices");

public JRadioButton(String text, boolean selected)
JRadioButton aRadioButton = new JRadioButton("8 slices", true);

public JRadioButton(String text, Icon icon)
JRadioButton aRadioButton = new JRadioButton("12 slices",  new DiamondIcon(Color.CYAN, false));
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));

public JRadioButton(String text, Icon icon, boolean selected)
JRadioButton aRadioButton = new JRadioButton("16 slices",  new DiamondIcon(Color.CYAN, false), true);
aRadioButton.setSelectedIcon(new DiamondIcon(Color.BLUE, true));

public JRadioButton(Action action)
Action action = ...;
JRadioButton aRadioButton = new JRadioButton(action);
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.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MainClass {

  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JRadioButtonDemo());
    f.setSize(500, 500);
    f.setVisible(true);
  }
}

class JRadioButtonDemo extends JPanel implements ActionListener {
  JTextField tf;

  public void init() {
    try {
      SwingUtilities.invokeAndWait(new Runnable() {
        public void run() {
          makeGUI();
        }
      });
    } catch (Exception exc) {
      System.out.println("Can't create because of " + exc);
    }
  }

  private void makeGUI() {

    setLayout(new FlowLayout());

    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);

    ButtonGroup bg = new ButtonGroup();
    bg.add(b1);
    bg.add(b2);
    bg.add(b3);

    tf = new JTextField(5);
    add(tf);
  }

  public void actionPerformed(ActionEvent ae) {
    tf.setText(ae.getActionCommand());
  }
}








14.9.JRadioButton
14.9.1.Creating JRadioButton Components
14.9.2.Using JRadioButtonUsing JRadioButton
14.9.3.Grouping JRadioButton Components in a ButtonGroupGrouping JRadioButton Components in a ButtonGroup
14.9.4.Determining the Selected JRadioButton in a Button Group
14.9.5.Listening to JRadioButton Events with an ActionListenerListening to JRadioButton Events with an ActionListener
14.9.6.Reference to the last selected item and check for reselectionReference to the last selected item and check for reselection
14.9.7.Listening to JRadioButton Events with a ChangeListenerListening to JRadioButton Events with a ChangeListener
14.9.8.Listening to JRadioButton Events with an ItemListenerListening to JRadioButton Events with an ItemListener
14.9.9.Event sequence: Action Event, Item Event and Change EventEvent sequence: Action Event, Item Event and Change Event
14.9.10.Getting selected JRadioButton in a containerGetting selected JRadioButton in a container
14.9.11.Customizing JRadioButton Look and Feel