Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.17. Radio buttons

5.17. Radio buttons
Last updated: 25 January 2013.

This tutorial will show you how to use radio buttons in Java.

The class JRadioButton inherits JToggleButton. Therefore, a radio button is a component that can be selected or deselected. In a group of radio buttons, only one radio button can be selected at a time. A group of radio buttons is an instance of the class ButtonGroup to which radio buttons are added.

Since the super class JToggleButton inherits AbstractButton, radio buttons have all the features provided by buttons. In particular, you can bind an icon, a tooltip text and a keyboard shortcut to a radio button. The picture shown below is a frame containing several radio buttons that allow the user to set the foreground color of a label:


Radio buttons


The code follows:


import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

public final class RadioButtons extends JFrame implements ActionListener {

   
private JRadioButton blackButton;
   
private JRadioButton redButton;
   
private JRadioButton greenButton;
   
private JRadioButton blueButton;

   
private JLabel label;

   
public RadioButtons(){
         
init();
          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
public void actionPerformed(ActionEvent e) {
         
if(e.getSource().equals(blackButton))
               
label.setForeground(Color.BLACK);
         
else if(e.getSource().equals(redButton))
               
label.setForeground(Color.RED);
         
else if(e.getSource().equals(greenButton))
               
label.setForeground(Color.GREEN);
         
else if(e.getSource().equals(blueButton))
               
label.setForeground(Color.BLUE);
   
}


   
private void addComponents(){
         
JPanel contentPane = new JPanel();
          contentPane.setLayout
(new BoxLayout(contentPane, BoxLayout.LINE_AXIS));

          ButtonGroup colorButtonGroup =
new ButtonGroup();

         
// The left pane contains the radio buttons --------------------------------------------------------
          // -----------------------------------------------------------------------------------------------------
         
JPanel leftPane = new JPanel(new GridLayout(0, 1));
          leftPane.setBorder
(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

          blackButton =
new JRadioButton("Black");
          blackButton.setSelected
(true);
          blackButton.addActionListener
(this);

          redButton =
new JRadioButton("Red");
          redButton.addActionListener
(this);

          greenButton =
new JRadioButton("Green");
          greenButton.addActionListener
(this);

          blueButton =
new JRadioButton("Blue");
          blueButton.addActionListener
(this);

          leftPane.add
(blackButton);
          leftPane.add
(redButton);
          leftPane.add
(greenButton);
          leftPane.add
(blueButton);

          contentPane.add
(leftPane);
         
// -----------------------------------------------------------------------------------------------------


          // The right pane contains the label ----------------------------------------------------------------
          // -----------------------------------------------------------------------------------------------------
         
JPanel rightPane = new JPanel();
          rightPane.setBorder
(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

          label =
new JLabel();
          label.setHorizontalAlignment
(SwingConstants.CENTER);
          label.setPreferredSize
(new Dimension(180, 130));
          label.setText
("Radio buttons example");

          rightPane.add
(label);

          contentPane.add
(rightPane);
         
// ----------------------------------------------------------------------------------------------------


          // Add the radio buttons to the ButtonGroup
         
colorButtonGroup.add(blackButton);
          colorButtonGroup.add
(redButton);
          colorButtonGroup.add
(greenButton);
          colorButtonGroup.add
(blueButton);


         
// Add the content pane to the JFrame
         
add(contentPane);
   
}


   
private void init(){
         
setTitle("Radio buttons");
          setSize
(300, 170);
          setLocationRelativeTo
(null);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new RadioButtons();
               
}
          })
;
   
}
}


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.17. Radio buttons
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.18. Menus

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7