Java Swing CheckBox Tutorial Using NetBeans, Summaries of Computer science

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

Typology: Summaries

2021/2022

Available from 01/12/2023

OusBoss
OusBoss 🇲🇦

113 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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 few food items along with their prices. User is asked to select the food items to purchase.
On selecting the food items, the bill amount for the selected food items will be displayed as shown in
below figure:
Figure 1
I am using NetBeans IDE for creating Java application. To create this application, follow the below
given steps:
Launch NetBeans, select File->New Project option.
The Choose Project dialog box will open up. Because, we want to make a Java desktop application,
select "Java" option from the Categories pane and "Java Application" option from the Projects pane.
Select Next button to move further. The next dialog box will ask the Java application's name and the
location on the drive where we want to create the application. Let us assign the name,
"checkBoxDemoApp" to this java application and let the location be the default location as shown in
below figure.
Figure 2
Click Finish button to create the application. The NetBeans IDE will automatically create a Java class
file for you by name, CheckBoxDemoApp.java with some default code. The Projects window will
appear as shown below:
Figure 3
To create a GUI (Graphical User Interface), we will be using Java Swing toolkit. To work with Java
Swing toolkit, you need to add a JFrame form to the application. Hence, right click the package,
checkboxdemoapp in the Source Packages node in the Projects window and select 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 us enter FastFood as the class name. Keeping the rest of text boxes in the
dialog box to their default values, click Finish button to create a JFrame form.
Figure 4
pf3
pf4
pf5

Partial preview of the text

Download Java Swing CheckBox Tutorial Using NetBeans and more Summaries Computer science in PDF only on Docsity!

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 few food items along with their prices. User is asked to select the food items to purchase. On selecting the food items, the bill amount for the selected food items will be displayed as shown in below figure: Figure 1 I am using NetBeans IDE for creating Java application. To create this application, follow the below given steps: Launch NetBeans, select File->New Project option. The Choose Project dialog box will open up. Because, we want to make a Java desktop application, select "Java" option from the Categories pane and "Java Application" option from the Projects pane. Select Next button to move further. The next dialog box will ask the Java application's name and the location on the drive where we want to create the application. Let us assign the name, "checkBoxDemoApp" to this java application and let the location be the default location as shown in below figure. Figure 2 Click Finish button to create the application. The NetBeans IDE will automatically create a Java class file for you by name, CheckBoxDemoApp.java with some default code. The Projects window will appear as shown below: Figure 3 To create a GUI (Graphical User Interface), we will be using Java Swing toolkit. To work with Java Swing toolkit, you need to add a JFrame form to the application. Hence, right click the package, checkboxdemoapp in the Source Packages node in the Projects window and select 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 us enter FastFood as the class name. Keeping the rest of text boxes in the dialog box to their default values, click Finish button to create a JFrame form. Figure 4

A Java class file, FastFood.java file will be added to the project with some default code. The Projects window will now appear as shown below: Figure 5 Also, a blank JFrame form will be created and opened in Editor window with a toolbar that contains several buttons. We will be working primarily with 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 on the form Source - This button will show the class's source code. We can write Java code for any component, edit, debug code etc. in this mode In this application, we will be requiring two Label components and three Check Box components. Three Check Box components will display three food items along with their price. Out of the two Label components, one will be used for displaying title and the second Label component will be used for displaying the bill amount of the selected food items. Select the JFrame form i.e. FastFood.java file from the Projects window followed by selecting the Design toolbar button from the Editor tab. Being in Design mode, drag a Label component from the Swing Controls category of the Palette window and drop it on the JFrame form. Repeat the procedure for the second Label component. Similarly drag a Check Box component from the Swing Controls category and drop it on the JFrame form. Repeat the procedure for two more Check Boxes because we want three Check Box components in this application. To increase visibility of these GUI components, we need to increase their font size. So, select all the five components i.e. Ctrl+click each component on the JFrame form and select font property from the Properties window. Select the desired font and size as shown in below figure: Figure 6 After increasing the font size, position the five components to appear as shown in below figure: Figure 7

on any Java Swing component in the JFrame form. On double clicking the component, you will be taken to the Source mode to write code for that component. We will begin writing code for jCheckBoxBurger component. So, double click the 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; } else { billAmount=billAmount-5; } jLabelResponse.setText("Bill Amount is $"+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 whenever some action is performed on the jCheckBoxBurger i.e. whenever it is checked or unchecked. In the jCheckBoxBurgerActionPerformed method, we determine if the checkbox is checked or not. If the checkbox is checked, integer value 5 is added to the billAmount variable. If the check box is unchecked, value 5 is subtracted from the billAmount variable.

After writing the code for jCheckBoxBurger, we need to write code for the jCheckBoxHotDog. So, from the Code mode, we will go back to the Design mode by clicking the Design toolbar button from the Editor window. From the Design mode, double click on the jCheckBoxHotDog and write the following code in the jCheckBoxHotDogActionPerformed method that is automatically created in the Code mode: private void jCheckBoxHotDogActionPerformed(java.awt.event.ActionEvent evt) { if (jCheckBoxHotDog.isSelected()) { billAmount=billAmount+7; } else { billAmount=billAmount-7; } jLabelResponse.setText("Bill Amount is $"+String.valueOf(billAmount)); } Again, go back to the Design mode by clicking the Design toolbar button. Double click on the jCheckBoxPizza and write the following code in the Code mode: private void jCheckBoxPizzaActionPerformed(java.awt.event.ActionEvent evt) { if (jCheckBoxPizza.isSelected()) {

Now you are ready to run the project. On running the project, you will get the following three food items in the form of check boxes to select from. Select any number of food items and you get their bill amount: