Checkboxes, Radio Buttons, and Combo Boxes

Radio buttons provide a user with a way to select one of a number of labeled options displayed simultaneously in a window.  Radio buttons are organized in a radio button group. Only one button in a group can be selected at any given time. When the user selects a new button, the previously selected button is automatically deselected. One button is usually selected at program starup by default.

Check boxes are labeled options that allow the user to select multiple options at once. Thus, any, all, or none of a set of check boxes may be selected.

Combo boxes are drop-down menus that can appear along with other widgets in the main area of a window. The user may select one option at a time, and it then remains selected.

The next screen shots show a program that allows the user to select options from all three types of controls.  The user can then view the states of the controls by clicking the Get states button.





Radio buttons, check boxes, and combo boxes are represented by the classes JRadioButton, JCheckBox, and JComboBox in javax.swing. Radio buttons belong to a radio button group, which is an instance of the ButtonGroup class.  In a BreezySwing program, the programmer first adds the radio buttons to the window, and then adds them to their group(s).  Check boxes and combo boxes are simply added to the window. Each type of control has methods for setting and accessing the control's state. The complete program which follows, in the file, shows these methods in action.


import javax.swing.*;

import BreezySwing.*;


public class CheckBoxDemo extends GBFrame{


    JLabel checkLabel = addLabel ("Checkbox state",1,1,1,1);

    JLabel radioLabel = addLabel ("Radio button state",2,1,1,1);

    JLabel comboLabel = addLabel ("Combo box state",3,1,1,1);

    JTextField checkField = addTextField ("", 1,2,2,1);

    JTextField radioField = addTextField ("", 2,2,2,1);

    JTextField comboField = addTextField ("", 3,2,2,1);


    JCheckBox cbDriver    = addCheckBox ("Driver", 4,1,1,1);      // Checkboxes

    JCheckBox cbPassenger = addCheckBox ("Passenger", 5,1,1,1);  


    ButtonGroup bgMaritalStatus = new ButtonGroup();              // Radio button group

    JRadioButton rMarried  = addRadioButton ("Married", 4,3,1,1); // Radio buttons 

    JRadioButton rSingle   = addRadioButton ("Single", 5,3,1,1);  

    JRadioButton rDivorced = addRadioButton ("Divorced", 6,3,1,1);


    JButton getStateBTN = addButton("Get states", 7,1,3,1);


    JComboBox comboHobbies = addComboBox(6,1,1,1);                // Combo box


    public CheckBoxDemo(){

        // Mark the default checkbox and radio button

        cbDriver.setSelected (true);

        rSingle.setSelected (true);

        // Add the radio buttons to the button group





        // Add items to the combo box

        comboHobbies.addItem ("Swimming");

        comboHobbies.addItem ("Reading");

        comboHobbies.addItem ("Golf");

        comboHobbies.addItem ("Fishing");

        comboHobbies.addItem ("Dusting");

        comboHobbies.addItem ("Cooking");

        comboHobbies.addItem ("Movies");

  (2);               // Select the 3rd string



    public void buttonClicked(JButton buttonObj){

        String cbStr = "", rStr = "";

        if (cbDriver.isSelected())

            cbStr = "Driver";

        if (cbPassenger.isSelected())

            cbStr = cbStr + "Passenger";


        if (rMarried.isSelected())

            rStr = "Married";

        else if (rDivorced.isSelected())

            rStr = "Divorced";

        else if (rSingle.isSelected())

            rStr = "Single";


        comboField.setText((String) comboHobbies.getSelectedItem());



    public static void main(String[] args){

        JFrame frm = new CheckBoxDemo();

        frm.setSize (400, 300);

        frm.setVisible (true);






Back to tutorial Next topic: Panels