[Java Swing] JRadioButton in Java – JRadioButton in java
Content
Create JRadioButton
Put JRadioButton group
Getting event JRadioButton
JRadioButton is an object that allows us to choose the attributes brittle as JCheckBox. However, we tend to use JRadioButton when the user wants to select only one of the attributes. For example, users choose the sex was just selected as M or F.
How to create JRadioButton – How to create JRadioButton
To create JRadioButton we use one of the following constructor:
– JRadioButton(): Create JRadioButton no text, not selected.
– JRadioButton(Action a): Create a JRadioButton event
– JRadioButton(Icon icon): Create JRadioButton with icon
– JRadioButton(Icon icon, boolean selected): Create JRadioButton the icon and set the selected or not
– JRadioButton(String text): Create JRadioButton with text
– JRadioButton(String text, boolean selected): Create JRadioButton with text and set to be selected or not
– JRadioButton(String text, Icon icon): Create JRadioButton with text and icons
– JRadioButton(String text, Icon icon, boolean selected): Create JRadioButton with text, icon and put option.
Now we will implement to create JFrame including 2 Boy and Girl is JRadioButton (Boy is selected by default) with 1 JLabel to their implementation show the results when selecting JRadioButton.
package nguyenvanquan7826.JRadioButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; public class MyRadioButton extends JFrame { private JRadioButton radBoy, radGirl; private JLabel lbGender; public MyRadioButton() { // add main panel add(createMainPanel()); // set display setTitle("JRadioButton"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(200, 100); setLocationRelativeTo(null); setVisible(true); } // create main panel private JPanel createMainPanel() { JPanel panel = new JPanel(); // add two radiobutton and a label to frame panel.add(radBoy = createRadioButton("Boy", true)); panel.add(radGirl = createRadioButton("Girl", false)); panel.add(lbGender = createLabel("You are a boy")); return panel; } // create a JRadioButton with name private JRadioButton createRadioButton(String name, boolean select) { JRadioButton rad = new JRadioButton(name, select); return rad; } // create a JLabel with text private JLabel createLabel(String text) { JLabel lb = new JLabel(text); return lb; } public static void main(String[] args) { new MyRadioButton(); } }
That has made JRadioButton, however, when you select your gender, there is no phenomenon happens with the JLabel, even at a time can select all 2 JRadioButton. So how to choose only one JRadioButton? how to capture the event for JRadioButton?
Put JRadioButton group – Set group for JRadioButton
To do that just allows the user to select one of the JRadioButton we need to put into a group JRadioButton – ButtonGroup. That is, we need to create a ButtonGroup and put them in there ButtonGroup. You edit the code of the method createMainPanel() as follows:
private JPanel createMainPanel() { JPanel panel = new JPanel(); // add two radiobutton and a label to frame panel.add(radBoy = createRadioButton("Boy", true)); panel.add(radGirl = createRadioButton("Girl", false)); panel.add(lbGender = createLabel("You are a boy")); // create ButtonGroup for radBoy and radGirl ButtonGroup genderGroup = new ButtonGroup(); // add radBoy and radGirl into Group genderGroup.add(radBoy); genderGroup.add(radGirl); return panel; }
So now we only select the Boy or Girl.
Getting event JRadioButton – Action on JRadioButton
Next event will be captured for JRadioButton, it is similar to capture the event for JCheckBox. We need to implements the interface ItemListener to perform addItemListener for JRadioButton. Following the write method itemStateChanged to capture events. Check a JRadioButton is selected by the method isSelected().
We update the method createRadioButton as follows:
private JRadioButton createRadioButton(String name, boolean select) { JRadioButton rad = new JRadioButton(name, select); rad.addItemListener(this); return rad; }
Next event is captured in the method itemStateChanged
private void changeSelect() { lbGender.setText("You are a " + (radBoy.isSelected() ? "boy" : "girl")); } @Override public void itemStateChanged(ItemEvent e) { changeSelect(); }
Our complete code as follows:
package nguyenvanquan7826.JRadioButton; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; public class MyRadioButton extends JFrame implements ItemListener { private JRadioButton radBoy, radGirl; private JLabel lbGender; public MyRadioButton() { // add main panel add(createMainPanel()); // set display setTitle("JRadioButton"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(200, 100); setLocationRelativeTo(null); setVisible(true); } // create main panel private JPanel createMainPanel() { JPanel panel = new JPanel(); // add two radiobutton and a label to frame panel.add(radBoy = createRadioButton("Boy", true)); panel.add(radGirl = createRadioButton("Girl", false)); panel.add(lbGender = createLabel("You are a boy")); // create ButtonGroup for radBoy and radGirl ButtonGroup genderGroup = new ButtonGroup(); // add radBoy and radGirl into Group genderGroup.add(radBoy); genderGroup.add(radGirl); return panel; } // create a JRadioButton with name private JRadioButton createRadioButton(String name, boolean select) { JRadioButton rad = new JRadioButton(name, select); rad.addItemListener(this); return rad; } // create a JLabel with text private JLabel createLabel(String text) { JLabel lb = new JLabel(text); return lb; } private void changeSelect() { lbGender.setText("You are a " + (radBoy.isSelected() ? "boy" : "girl")); } @Override public void itemStateChanged(ItemEvent e) { changeSelect(); } public static void main(String[] args) { new MyRadioButton(); } }
Read more: class JRadioButton, use JRadioButton
let me ask :even when I have an array JRadioButton, how to catch events for each element in the array is ItemListenner?
If they can catch as many normal button ice addActionListener way for elements.
cho em hỏi em có 3 nút button khi em kick vào nhưng cái khác nhau thì các JRadioButton sẽ thay đổi thì phải làm như thế nào à.
Bạn bắt sự kiện cho nó. Trong onclick thay đổi radio select là được.
em vẫn không hiểu anh à 😀 anh có thể viết ra cho em xem được không
a ơi cho e hỏi với ạ. để bắt sự kiện cho radiobutton mà khi ấn chọn sẽ mở ra một giao diện khác thì như thế nào ạ?
You do like to JButton
My e used a RadiobuttonMenuItem than using multiple buttons. e began this that runs n k out. @Override
public void itemStateChanged(ItemEvent and) {
if(rb1.isSelected()){
LopClient1 c=new LopClient1();
}
}
You try addActionPerfrom view
Oh she appeared he wanted to reset the default values for both 2 button a.k false, how to initialize a nhe.e initial value set to star as his wish to initiate the ạ
You setSelect for it offline
The article clearly understandable tutorial. thanks alot