Java Swing CheckBox Tutorial Using NetBeans

In this tutorial, you will learn to use Java Swing checkboxes using Netbeans IDE. This java application will show some food items along with their prices. The user is asked to select the food items to buy. On selecting the food items, the bill amount for the selected food items will be displayed as shown in the figure below:

figure 1

I am using NetBeans IDE to build a Java application. To create this application, follow the steps below:

  1. Launch Netbeans, choose File->New Project option.
  2. The Choose Project dialog box will open. Because, we want to create a Java desktop application, select the “Java” option from the Category pane and the “Java Application” option from the Projects pane. Select the Next button to proceed. The next dialog box will ask the name of the Java application and the location on the drive where we want to create the application. Let us name this Java application as “checkBoxDemoApp” and let the location be the default location as shown in the figure below.

Figure 2

  1. Click on Finish button to create the application. The NetBeans IDE will automatically create a Java class file for you named CheckBoxDemoApp.java with some default code. The project window will appear as shown below:

Figure 3

To create the GUI (Graphical User Interface), we will be using the Java Swing Toolkit. To work with the Java Swing Toolkit, you need to add a JFrame form to the application. So, right click the package, checkbox DemoApp in the Source Package node in the Project window and choose New->JFrame Form option from the context menu that pops up. You will be asked to specify the class name for the new JFrame form. Let’s enter fastfood as the class name. Click the Finish button to create the JFrame form, keeping the rest of the text boxes in the dialog box at their default values.

Figure 4

A Java class file, the FastFood.java file, will be added to the project along with some default code. The Project window will now appear as shown below:

Figure 5

Additionally, an empty JFrame form will be created and opened in the editor window with a toolbar containing several buttons. We’ll be working primarily with the following two toolbar buttons:

  • design – This button will enable us to work with GUI components. We can drag GUI components from the palette window and drop them onto the form
  • Source – This button will show the source code of the class. We can write, edit, debug code etc. Java code for any component. in this mode

In this application, we will need two Label components and three Check Box components. The three check box components will display the three food items along with their price. One of the two label components will be used to display the title and the other label component will be used to display the bill amount of the selected food items.

Select the JFrame form i.e. FastFood.java file from the Project window and then select the Design toolbar button from the Editor tab. While in design mode, drag a Label component from the Swing Control category of the Palette window and drop it onto the JFrame form. Repeat the process for the second label component. Similarly, drag a check box component from the Swing control category and drop it onto the JFrame form. Repeat the process for two more check boxes as we want three check box components in this application. To increase the visibility of these GUI components, we need to increase their font size. So, select all the five components i.e. Ctrl+click on each component on the JFrame form and select the Font property from the properties window. Select the desired font and size as shown in the image below:

Figure 6

After increasing the font size, display the five components as shown in the figure below:

Figure 7

The next step is to change the text of these components. You can change the text of any component in the following two ways:

  • Right click on the component on the JFrame form and select Edit Text option from the context menu that pops up. You can overwrite the text of a component by pressing the Enter key
  • Click on Component on the JFrame Form and select the Text property from the Properties window. Type the desired substance in the component’s Text property.

Using any of the above methods, let’s change the text property of the components as below:

  • Set the text of the first label component to “Select Foods”.
  • Set the text of the first check box to “Burger $5”.
  • Set the text of the second check box to “Hot Dog $7”.
  • Set the text of the third check box to “Pizza $10”
  • Remove the Text property of the second Label component as its Text property will be set via Java code. Through this label component we will display the total bill amount of the selected food items.

After setting the Text property of all five components, the JFrame form will appear as shown below:

figure 8

Every Java Swing component that we drop on a JFrame form is given a default variable name such as jLabel1, jLabel2, jCheckBox1, jCheckBox2 and so on. We need to assign meaningful variable names to these components. To assign a variable name to any component, simply right click on it and choose Change Variable Name option from the context menu that opens. Let’s set the variable name of the first check box to jCheckBoxBurger, the second check box to jCheckBoxHotDog and the third check box to jCheckBoxPizza. Also, set the second Label component’s variable name to jLabelResponse.

We want that whenever a checkbox is checked or unchecked, the bill amount of the selected checkbox will be displayed.

write code

Currently, we are in design mode and in order to write the code, we need to switch to source mode. To switch to source mode, either you click on the Source toolbar button from the editor window or double click on any Java Swing component in the JFrame form. On double clicking on the component, you will be taken to source mode to write the code for that component. We’ll start writing the code for the jCheckBoxBurger component. So, double click on jCheckBoxBurger and you will be taken to source mode. Write the following code:

int billAmount = 0;

private void jCheckBoxBurgerActionPerformed(java.awt.event.ActionEvent evt) {

if (jCheckBoxBurger.isSelected()) {

BillAmount=BillAmount+5;

} Otherwise {

BillAmount=BillAmount-5;

,

jLabelResponse.setText(“BillAmount$” + String.valueOf(billAmount));

,

Description of above code

You define a global integer variable called BillAmount and initialize it to 0. The jCheckBoxBurgerActionPerformed method will be invoked when some action is performed on the jCheckBoxBurger i.e. whenever it is checked or unchecked. In the jCheckBoxBurgerActionPerformed method, we determine whether the checkbox is checked or not. If the checkbox is checked, the integer value 5 is added to the BillAmount variable. If the check box is unchecked, a value of 5 is subtracted from the BillAmount variable.

After writing the code for jCheckBoxBurger, we need to write the code for jCheckBoxHotDog. So, from Code Mode, we will go back to Design Mode by clicking on the Design Toolbar button from the Editor window. From design mode, double click on jCheckBoxHotDog and in code mode write the following code in the automatically created jCheckBoxHotDogActionPerformed method:

private void jCheckBoxHotDogActionPerformed(java.awt.event.ActionEvent evt) {

if (jCheckBoxHotDog.isSelected()) {

BillAmount=BillAmount+7;

} Otherwise {

BillAmount=BillAmount-7;

,

jLabelResponse.setText(“BillAmount$” + String.valueOf(billAmount));

,

Again, return to Design Mode by clicking the Design Toolbar button. Double click on jCheckBoxPizza and type the following code in code mode:

private void jCheckBoxPizzaActionPerformed(java.awt.event.ActionEvent evt) {

if (jCheckBoxPizza.isSelected()) {

BillAmount=BillAmount+10;

} Otherwise {

BillAmount=BillAmount-10;

,

jLabelResponse.setText(“BillAmount is $” + String.valueOf(BillAmount));

,

After writing the code for all three check boxes, our project is ready to run. You can run the project by choosing one of the following three options:

  • Selecting the Run Project Toolbar button from the toolbar at the top
  • press the F6 key
  • Select Run->Run Project option from the menu bar

If you run the project now, you will not get any output because at this time the main class of the project is CheckBoxDemoApp.java and we have not written any code in this java file. So, we first need to replace the main class file. So, right click on the checkboxDemoApp project in the project window and choose Properties option from the context menu that pops up. From the Properties dialog, select the Run category from the Categories pane. In the Main class textbox, you will find the text, CheckboxDemoApp.CheckboxDemoApp which confirms that when running the project, the CheckBoxDemoApp.java file will be executed first. Change the content in Main class textbox to CheckboxDemoApp. To indicate that you want to run the fastfood.java file first whenever the project is run as shown in the figure below

Figure 9

Now you are ready to run the project. Upon running the project, you will get the following three food items to choose from as check boxes. Select any food item and you get their bill amount:

Figure 10

You can download the complete project from the following link: http://bmharwani.com/checkBoxDemoApp.zip

Leave a Comment

Your email address will not be published.