Skip to main content

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 40

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 39 | Lesson 41 >>

Lesson 40: Working with GUI controls: Check box and Text area

Welcome to Lesson 39 of Programming in Java NetBeans, we’ll continue learning more about the Graphical User Interface (GUI) controls provided in Java programming language and specifically in Java NetBeans. In this lesson, we shall discuss Check box GUI control.

In the previous lesson, we did learn about Combo box, a GUI control that allow user to select an item from a drop-down list. Selecting items rather than typing quickens the process of data entry and Java NetBeans provides a number of Controls that enhance user selection of items.


Check box GUI control

A Check box is a control that allows users to choose items by way of selecting or deselecting a tick box provided in a Check box control. A group of Check boxes, unlike Radio Buttons, can allow users to select more than one item.

The best method of working with a group of Check boxes is to put them on a Panel control, that way one can manipulate them as a group rather than individually. A Panel is a Swing container that can hold other controls.

Open the form we have been using since Lesson 36. On this form, we’ll add a sizable Panel that will contain the group of Check boxes. Drag the Panel from the Palette window under the Swing containers and drop it on the form.

Palette window


We are going to have a group of five Check boxes each Check box representing an international language. We’ll re-name the Check boxes variable names to ChkBox1 through ChkBox5 and then change the caption of each Check box from the default “jCheckBox1” to a specific language, for example English. See below:


The group of Check boxes is almost done but don't you think it would be better if we let the user know what he or she is expected to do with the languages.

We’ll add a titled border for the Panel control with a title “Select language” so that the user can select preferred language or languages.

Click on the Panel itself so that it is selected, this will ensure that only the properties that apply to the Panel are active on the Properties window.

On the Properties window, click on border properties and from the dialog window that will popup, select “Titled Border”, enter title as “Select language” and choose and other properties such as font.


Here is the complete and run form with two of the languages selected:


Text area GUI control

We’ll add a Text area control on the form so that when the user selects a language or languages they will be listed on the Text area. Drag and drop a Text area control onto the form from the Palette window under the Swing controls. We’ll re-name this Text area as “TxtLanguages” since it will contain languages.

Scroll to Continue

The form should look as shown when run with two languages still selected. Don’t worry about the scroll bar at the bottom of the Text area. The scroll bar will only appear if the Text area is smaller enough.


We need to know which Check box was selected so that we can add its caption text into the Text area as the selected language. We’ll use an IF statement.

For us to drop the selected language into the Text area, we’ll use a command button which will contain the code with IF statement.

Add a command button into the form as shown below, you might need to re-size the form and the Text area for the controls to fit as shown.

Change the caption of the command button to “Add” since we want the button to add whichever language was selected into the Text area.


The Command button will contain the code with the IF statement. Java Check boxes have a property called isSelected() that return a Boolean true if the Check box is selected or a Boolean false if the Check box is not selected. We can use this property in a series of IF Statements to see if each Check box is selected or not. If they are, we can build up a string, adding the text caption from each Check box. Double click the Command button to enter the following code.


Discover more on Java ...

We have set a string variable called lg. If a Check box is selected then we get the caption text of that Check box. This text is then stored in the lg variable, along with a new line character ( '\n')

The last line of code sets the text for the Text area and we are passing this string of text inside the round bracket of setText() method. Running the form now you should have the button adding the language or languages selected into the Text area.


As you can see, the Command button can be able to add the selected language or languages into the Text area. Every time it is clicked the series of IF statements will be executed to reflect the current selected Check boxes. In the next lesson, we shall discuss Radio button GUI control.

<< Lesson 39 | Lesson 41 >>

Want to make money online?


Danson Wachira (author) from Nairobi, Kenya on January 29, 2013:

Hi yoesef,

Thanks for the visit and comment, i have just added Lesson 41, please check.

yoesef on January 28, 2013:

where is lesson 41?

Max on January 13, 2013:

This tutorial is great, just what i needed for my classes, thanks man! May you write more articles on this subject.

Related Articles