JCheckBox JRadioButton Example Java Swing


JCheckBox JRadioButton: The Swing GUI components contain three types of state button s— JToggleButton, JCheckBox and JRadioButton — that have on/off (or true/false) values. JToggleButtons are frequently used with toolbars (sets of small buttons typically located on a bar across the top of a window). Classes JCheckBox and JRadioButton are subclasses of JToggleButton.

Following is the hierarchy.

JCheckBox JRadioButton

In the following JCheckBox JRadioButton program, checkboxes and radiobuttons are added to two different panels. The panels are placed in frame. The state of the JCheckBox and JRadioButton is evaluated with the method isSelected() like getState() method of AWT Checkbox.

import javax.swing.* ;    
import java.awt.* ;   
import java.awt.event.* ;
public class CheckRadio extends JFrame implements ItemListener  
{			
  JPanel northpanel, southpanel;			// declare reference variables
  JTextField jfield;
  JCheckBox pastrybox, burgerbox, pizzabox;
  JRadioButton veg, nonveg;
  ButtonGroup bg;				        // to group radio buttons

  public CheckRadio ( )   
  {
    northpanel = new JPanel( ); 			// for adding checkboxes
    southpanel = new JPanel( );                                          // for adding radio buttons
    northpanel.setBackground(Color.pink);               // only for attractive display
    southpanel.setBackground(Color.magenta);

    jfield = new JTextField("Click and See");           // for displaying the state of buttons
    jfield.setBackground(Color.cyan);

    pastrybox = new JCheckBox("Pastry");                // instantiate all checkboxes
    burgerbox = new JCheckBox("Burger");
    pizzabox = new JCheckBox("Pizza");

    veg = new JRadioButton("Vegetarian");               // instantiate all radiobuttons
    nonveg = new JRadioButton("Non-Vegetarian", true);  // displayed in selected state

    bg = new ButtonGroup( );                            // a button group to add all radio buttons
    bg.add( veg ) ;  bg.add( nonveg ) ;                              // add radio buttons to the group
					                // add checkboxes one by one to panel
    northpanel.add(pastrybox);    northpanel.add(burgerbox);    northpanel.add(pizzabox);
    southpanel.add(veg);          southpanel.add(nonveg);                                                  

                                                        // observe: group is not added as ButtonGroup is not a component
    pastrybox.addItemListener(this);                    // add listener to each JComponent
    burgerbox.addItemListener(this);  pizzabox.addItemListener(this); 
    veg.addItemListener(this);        nonveg.addItemListener(this); 

    Container c = getContentPane();
    c.add(northpanel, BorderLayout.NORTH);
    c.add(southpanel, BorderLayout.SOUTH);
    c.add( jfield, BorderLayout.CENTER);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	// to close the JFrame
  }
  public void itemStateChanged(ItemEvent e)  
  {
    String str = "";
    if(veg.isSelected())	  str = "You are Vegetarian";	
    else if(nonveg.isSelected()) str = "You are Non_Vegetarian";	

    jfield.setText("Pastry: " + pastrybox.isSelected() + "  Burger: " + burgerbox.isSelected() + "  Pizza: " + pizzabox.isSelected() + "  AND  " + str);
  }
  public static void main(String ... args)   
  {                                                     // instead of writing in Constructor, write in main() method
    JFrame f = new CheckRadio();
    f.setSize(400, 200);    
    f.setVisible(true);
  }
}

JCheckBox JRadioButton

JCheckBox is equivalent Checkbox of AWT. JRadioButton is new introduction in Swing. To group the radio buttons, ButtonGroup is used. ButtonGroup is equivalent to ChekcboxGroup of Java AWT.

isSelected() method is used to know which check box or radio button is selected by the user. The method returns true if selected and false is deselected. isSelected() is equivalent to getState() method of AWT Checkbox.

The state of the checkboxes and radio buttons is displayed in JTextField.

Leave a Comment

Your email address will not be published.