1. Radio ToggleButton
In Java Swing, we can make JToggleButton acting as a Radio Button. Just like in the previous example, we must create the JToggleButton and then assign that to the ButtonGroup. Doing so will make only one button in the group to switch to ON state. When a button is switched to ON state, all other buttons in the group go to the OFF state.
2. About the Example
The below screen shows the example:

In this example, we have seven JToggleButton components. First 4 is for the Nationality and second three is for the Sex. Here, these toggle buttons act like a radio button. One can also note that there are two groups here. In a group, only one toggle button can stay in Switched ON state. The above picture shows France and Female is in Selected State. Now, we will implement this example.
3. Create Button Groups
Since we need to group the Toggle Buttons in two groups, we create two ButtonGroup instances (Line 12,13). Later, we will add the ToggleButtons to this group just like what we do for the radio buttons.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class JFrameDemo extends JFrame { public JFrameDemo(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 400, 110); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Two Groups for Toggle Button ButtonGroup bgSex = new ButtonGroup(); ButtonGroup bgNationality = new ButtonGroup(); |
4. Setup Radio JToggleButton
In code snippet 3, we create three
JToggleButton and add that to our first button group. In code snippet 4, we do the same for the next set of JToggleButtons and group them under the Nationality
ButtonGroup. Now, we have two set of Radio JToggleButton in our sample. As already told, in a group, user can keep only one JToggleButton
in the selected state.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//Sample 03: Setup Gender JLabel lblSex = new JLabel("Sex"); JToggleButton btnMale = new JToggleButton("Male"); JToggleButton btnFemale = new JToggleButton("Female"); JToggleButton btnOther = new JToggleButton("Other"); bgSex.add(btnMale); bgSex.add(btnFemale); bgSex.add(btnOther); //Sample 04: Setup Nationality JLabel lblNationality = new JLabel("Nationality"); JToggleButton btnFrench = new JToggleButton("France"); JToggleButton btnIndian = new JToggleButton("India"); JToggleButton btnUSA = new JToggleButton("American"); JToggleButton btnRussian = new JToggleButton("Russia"); bgNationality.add(btnFrench); bgNationality.add(btnIndian); bgNationality.add(btnUSA); bgNationality.add(btnRussian); |
Finally, we add all the components to the content pane of the JFrame window.
1 2 3 4 5 6 7 8 9 10 |
//Sample 05: Add the Toggle group to Content Pane add(lblNationality); add(btnFrench); add(btnIndian); add(btnUSA); add(btnRussian); add(lblSex); add(btnMale); add(btnFemale); add(btnOther); |
5. Watch JToggleButton as Youtube
6. Code Reference
6.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { //Sample 07: Create Instance of JFrameDemo JToggleButtonAsRadio frame = new JToggleButtonAsRadio("JToggleButton as Radio Example"); frame.setVisible(true); } } |
6.2 JToggleButtonAsRadio.Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package tube.coding.examples; import java.awt.Container; import java.awt.FlowLayout; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JToggleButton; import javax.swing.SwingConstants; public class JToggleButtonAsRadio extends JFrame { public JToggleButtonAsRadio(String title) throws HeadlessException { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 400, 110); Container ControlHost = getContentPane(); ControlHost.setLayout(new FlowLayout()); //Sample 02: Create Two Groups for Toggle Button ButtonGroup bgSex = new ButtonGroup(); ButtonGroup bgNationality = new ButtonGroup(); //Sample 03: Setup Gender JLabel lblSex = new JLabel("Sex"); JToggleButton btnMale = new JToggleButton("Male"); JToggleButton btnFemale = new JToggleButton("Female"); JToggleButton btnOther = new JToggleButton("Other"); bgSex.add(btnMale); bgSex.add(btnFemale); bgSex.add(btnOther); //Sample 04: Setup Nationality JLabel lblNationality = new JLabel("Nationality"); JToggleButton btnFrench = new JToggleButton("France"); JToggleButton btnIndian = new JToggleButton("India"); JToggleButton btnUSA = new JToggleButton("American"); JToggleButton btnRussian = new JToggleButton("Russia"); bgNationality.add(btnFrench); bgNationality.add(btnIndian); bgNationality.add(btnUSA); bgNationality.add(btnRussian); //Sample 05: Add the Toggle group to Content Pane add(lblNationality); add(btnFrench); add(btnIndian); add(btnUSA); add(btnRussian); add(lblSex); add(btnMale); add(btnFemale); add(btnOther); } } |
Categories: Swing
Tags: ButtonGroup, JToggleButton, Radio