Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 39
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.
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.
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.
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:
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.
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.
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.
The form now should look like shown below:
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:
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.
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.
Other related Hubs...
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 2
In this lesson you will learn how to create your first Java project and how to interpret the NetBeans code editor window.
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 11
The WHILE loop in Java execute a statement or a group of statements so long as the specified condition remains TRUE. The loop starts with the keyword “while”. Just after the keyword you open the round brackets and specify the condition to be tested.
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 19
In this article we shall look at how we can take advantage of some of the many methods that ship in with Java programming language to manipulate data of string type. In particular we shall look at methods used to convert strings into uppercase and...
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 3
In Lesson 2, we saw how the Java code window looks like and we were able to interpret the various parts of the Java code. In this lesson we’ll learn how to write our first running Java program.
- Programming In Java NetBeans - A Step By Step Tutorial For Beginners: Lesson 10
The FOR loop is one of the most common looping controls used in Java. The FOR loop forces the program to repeat a statement or a group of statements a specified number of times. It has three parts; the initial value part, condition part, and ...