Updated date:

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

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

Table of Contents

<< Lesson 38 | Lesson 40 >>

Lesson 39: Working with GUI controls: Combo Box

In Lesson 37 we learnt how we can use Label and Text field GUI controls on a form and in Lesson 38 we covered different ways of manipulating Command buttons. We’ll continue learning more about GUI controls in Java NetBeans and in this lesson we shall learn how to work with Combo Box Swing controls.

When you have options to be selected by the user in a program, it would be easier if you present those options in one of the user selection GUI controls provided in Java NetBeans. These controls not only do they make data entry fast but they also limit the number of errors in data entry since the user has to select pre-defined options.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-39

Combo Box GUI control

Combo Box is a drop down list of items that can be selected by a user by way of clicking. Since the list of items is hidden until the user clicks on Combo box, this control is therefore used in situation where you need to use least space on a form.

On the Palette window under Swing controls, locate for a Combo box control. Drag and drop it onto the form. Rename the Combo box by right-clicking and choosing “Change Variable Name” as we did with the previous controls. We’ll call it “cboCountry” since we want to store a list of countries in it.

Palette window

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-39
programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-39

The default items in the Combo Box are listed as Item 1, Item 2, etc. We'll add our own list of items consisting of names of countries. Select the Combo box by clicking on it and then look for its Model property under the Properties window.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-39

Click on the small dotted button to the right of the model row to reveal a dialog box that will enable you to change the list of items:

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-39

Delete the items which are currently shown on the dialog box and replace them with a list of at least ten names of countries. Each country name should occupy a single row.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-39

Click “OK” to make the list changes, now run the form and the Combo box should be able to drop a list of the countries.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-39

Now that we have the Combo box working, we need to learn a few ways of manipulating it. We want a Text field to display the name of the country that the user selects from the Combo box. To do this, we’ll need another Text field on the form. Go ahead and add a new Text field on form. We’ll call this new Text field “txtComboValue” since it will hold the value selected from the Combo box.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-39

The form now should look like shown below:

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-39

To have the item selected in the Combo box displayed in the Text field, we need to have a code in the Combo box. The code in the Combo box will take the item from the Combo box and set it as text in the Text field. We can use the getSelectedItem() method of the Combo box.

Items listed in the Combo box are stored as objects and as such, we need to convert them into real strings which can be displayed in the Text field. To convert the items of Combo box from objects to strings we can use type casting.

Casting means converting from one variable type to another and that is what we should do. Double click the Combo box to reveal the code and locate the ActionPerformed event method of the Combo box among the code. Enter the following code:

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-39

From the code shown above, we are getting the current item on the Combo box and setting it as a text in the Text field after type casting. To type cast an object in string, we just add the variable type surrounded by two round brackets. Notice the (String) before cboCountry.getSelectedItem(). Run the form now and you should be able to display the selected Combo box item in the Text Field.

programming-in-java-netbeans-a-step-by-step-tutorial-for-beginners-lesson-39

Although there are many ways in which we can manipulate a Combo box, this is just one of them. Go ahead and do some practice on different ways of manipulating the Combo box. See if you can be able to add items to a Combo box at run time using the addItem() method of Combo box. In the next lesson, we shall discuss Check Box GUI controls.

<< Lesson 38 | Lesson 40 >>

Other related Hubs...

Want to make money online?

Comments

Danson Wachira (author) from Nairobi, Kenya on December 20, 2012:

Yes Peter, programming is everywhere, on your mobile, computer, microwave.....everywhere. Thanks for the visit and comment.

Peter V from At the Beach in Florida on December 20, 2012:

Great tutorial. I am not very experienced in programming but I recognize it holds huge potential so the more people who understand and program the better!

Related Articles