JCheckBox JRadioButton Example Java Swing

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.

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 Reply

Your email address will not be published. Required fields are marked *