The ButtonGroup component is to compose a single-choice button to ensure that only one of the buttons is selected. You can add any set of objects that inherit from AbstractButton. This tutorial will show you how to create a single choice button using ButtonGroup and detect if the value is changed.
You will learn how to:
- Create a ButtonGroup and its use
- Creating an event handler to detect if the value is changed
Prerequisites
Before you begin to read this tutorial, you must:
- Familiar with Java swing development, go to oracle Java Swing to get tutorials reference.
- Read the first three tutorials on JSwing.
- Familiar with the Operation of Apache NetBeans.
- Familiar with the timing and object of itemStateChanged be called.
Create a SingleChoice Form
- Create a JFrame class named SingleChoice on the form of the packages, and then add two JLabels and JCheckBoxs, one JPanel, JTextField, and one ButtonGroup to the JFrame, JCheckBox must join the button group to make a single-choice button, the tree diagram as shown in the following figure.
- Change the properties of the component as shown in the table below.
Components text actionCommand name buttonGroup jLbael1 You are jLbael2 avatar jTextField1 CheckBoxValue jCheckBox1 Male Male Male buttonGroup1 jCheckBox2 Female Female Female buttonGroup1 - After the successful step above, Press Ctrl-S to save the file, and press F9 to Compile the Form, shown as the following.
Create a SingleChoice App
Create a Java class named SingleChoice on the app of the Packages, and then add code to the Class.
package app;
import jswing.application.*;
public final class SingleChoice extends App {
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Form.open("form.SingleChoice","SingleChoice");
UserInterface.initialize("app.SingleChoice",Form.getCurrentForm());
}
});
}
@Override
public void itemStateChanged(java.awt.Component component) {
if (component.equals(Form.getCurrentForm().getComponentByName("Male")) ||
component.equals(Form.getCurrentForm().getComponentByName("Female"))) {
Boolean a = ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Male")).isSelected();
Boolean b = ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Female")).isSelected();
if (a || b) {
System.out.println("valueChanged");
String s = (a) ? "Male" : "Female";
((javax.swing.JTextField)Form.getCurrentForm().getComponentByName("CheckBoxValue")).setText(s);
if (a) {
((javax.swing.JLabel)Form.getCurrentForm().getComponentByName("avatar")).setIcon(new javax.swing.ImageIcon("C:\\Users\\hsichi.jam.DC\\Documents\\NetBeansProjects\\JSwingApplication\\resources\\images\\man.png"));
} else {
((javax.swing.JLabel)Form.getCurrentForm().getComponentByName("avatar")).setIcon(new javax.swing.ImageIcon("C:\\Users\\hsichi.jam.DC\\Documents\\NetBeansProjects\\JSwingApplication\\resources\\images\\woman.png"));
}
Form.getCurrentForm().getFrame().pack();
}
}
}
@Override
public void actionPerformed(java.awt.event.ActionEvent e){
String action = e.getActionCommand();
}
}
Code Explained
- itemStateChanged.
ItemStateChanged gets invoked when an item has been selected or deselected by the user, so we use it to decide which avatar of the gender should be displayed.
Run application
Press Ctrl-S to save the file, Shift-F6 to Run the application, and then when you select a gender, the avatar will be displayed by choice.
Exercises
- Make the same application and replace JCheckBox with JRadioButton.
- Build an application to show images using JComboBox.
Next steps
Click download source code or next for more tutorials.