-The JRadioButton in java swing is an object that allows us to choose attributes like JCheckBox. However, we often use the JRadioButton in java swing when we want the user to select only one of the properties. For example, select a gender, the user is only selected Male or Female.
- JRadioButton (): Create JRadioButton without text, not pre-selected.
- JRadioButton (Action a): Create JRadioButton with event a
- JRadioButton (Icon icon): Create JRadioButton with icon
- JRadioButton (Icon icon, boolean selected): Create JRadioButton with icon and set whether selected
- JRadioButton (String text): Create a JRadioButton with text
- JRadioButton (String text, boolean selected): Create JRadioButton with text and set whether selected
- JRadioButton (String text, Icon icon): Create JRadioButton with text and icon
- JRadioButton (String text, icon icon, boolean selected): Create JRadioButton with text, icon and set selection.
-Now we are going to create a JFrame consisting of two JRadioButton in java swing, Boy and Girl (the default boy) and a JLabel to display the result when selecting JRadioButton.
-The JRadioButton in java swing has been created, but when you select a gender, nothing happens to JLabel, even at the same time you can select both JRadioButton. So how to select only one JRadioButton? How to capture events for JRadioButton?
-So now, we can only choose Boy or Girl. (Remember add library "import javax.swing.ButtonGroup;" ).
-We modify the createRadioButton method as follows:
-Next is to capture the event in the itemStateChanged method
-Our complete code is as follows:
-File name: MyRadioButton.java
-Continue MyRadioButton.java
-The output:
-Read more how to customize java swing borderlayout with detail example:
How to create JRadioButton in java swing
To create a JRadioButton in java swing we use one of the following constructors:- JRadioButton (): Create JRadioButton without text, not pre-selected.
- JRadioButton (Action a): Create JRadioButton with event a
- JRadioButton (Icon icon): Create JRadioButton with icon
- JRadioButton (Icon icon, boolean selected): Create JRadioButton with icon and set whether selected
- JRadioButton (String text): Create a JRadioButton with text
- JRadioButton (String text, boolean selected): Create JRadioButton with text and set whether selected
- JRadioButton (String text, Icon icon): Create JRadioButton with text and icon
- JRadioButton (String text, icon icon, boolean selected): Create JRadioButton with text, icon and set selection.
-Now we are going to create a JFrame consisting of two JRadioButton in java swing, Boy and Girl (the default boy) and a JLabel to display the result when selecting JRadioButton.
-File name: MyRadioButton.java
-Continue MyRadioButton.java-The JRadioButton in java swing has been created, but when you select a gender, nothing happens to JLabel, even at the same time you can select both JRadioButton. So how to select only one JRadioButton? How to capture events for JRadioButton?
Set group for JRadioButton in java swing
-To do that only allows the user to select one of the JRadioButton in java swing we need to put the JRadioButton into a group - ButtonGroup. That is, we need to create a ButtonGroup and put them into that ButtonGroup. You revised the code of the createMainPanel () method as follows:-So now, we can only choose Boy or Girl. (Remember add library "import javax.swing.ButtonGroup;" ).
Getting events for JRadioButton in java swing
-The next will be the event capture for the JRadioButton in java swing, it's similar to catching the event for the JCheckBox. We need to implements the ItemListener interface to implement addItemListener for the JRadioButton. Next, write the itemStateChanged method to capture the event. Check whether a JRadioButton is selected with the isSelected () method.-We modify the createRadioButton method as follows:
-Next is to capture the event in the itemStateChanged method
-Our complete code is as follows:
-File name: MyRadioButton.java
-Continue MyRadioButton.java
-The output:
-Read more how to customize java swing borderlayout with detail example:
Comments
Post a Comment