Make a single choice button on JSwing

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:

  1. Familiar with Java swing development, go to oracle Java Swing to get tutorials reference.
  2. Read the first three tutorials on JSwing.
  3. Familiar with the Operation of Apache NetBeans.
  4. Familiar with the timing and object of itemStateChanged be called.

 

Create a SingleChoice Form

  1. 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.

    SingleChoice tree view

  2. Change the properties of the component as shown in the table below.
    ComponentstextactionCommandnamebuttonGroup
    jLbael1You are   
    jLbael2  avatar 
    jTextField1  CheckBoxValue 
    jCheckBox1MaleMaleMalebuttonGroup1
    jCheckBox2FemaleFemaleFemalebuttonGroup1
  3. After the successful step above, Press Ctrl-S to save the file, and press F9 to Compile the Form, shown as the following.

    SingleChoice Form

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

  1. 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.

SingleChoice

 

Exercises

  1. Make the same application and replace JCheckBox with JRadioButton.
  2. Build an application to show images using JComboBox.

Next steps

Click download source code or next for more tutorials.

Share this page

Let more people know about this site, please share