Create Software Components 

Using Java Level 2

 

 

Course Info

Scheme

Resources

Tutorials

Java Demos

Utilities

Links


 

   Swing

JComboBox

This tutorial describes the Swing JComboBox class.

JComboBox Class

Uneditable JComboBox

Editable JComboBox

Other JComboBox Methods

 


The JComboBox Class

A combo box exists to provide the user with a list of items they can choose from.  A JComboBox comes in two very different forms: uneditable and editable.  

By default, a JComboBox is uneditable.  If you want an editable JComboBox you have to explicitly set it to be editable in your code using the setEditable method.

 

Uneditable JComboBox   

 

EditableJComboBox

                                         

Here are two constructors that can be used to create a JComboBox:-

  • JComboBox()

Creates a combo box with no items in the menu list

  • JComboBox(Object[])

Creates a combo box with items in the menu list specified by the Object array

 


Uneditable JComboBox     

By default, a combo box is not editable when it is first created.  This means the user cannot type values into the main box ate the top. When the user presses or clicks it, the combo box displays a menu of items to choose from.  You would use an uneditable combo box to display one-of-many choices when space is limited or when the number of choices is large. Other components that display one-of-many choices are lists.

 The applet below shows an uneditable combo box.

~~~Applet~~~

 To create an uneditable combo box, we can use code such as:- 

String[] familyStrings = { "Me - Looking Good!", 

                           "Boyfriend - Gorgeous hmm!", 

                           "Mum", "Dad", 

                           "Brother - Ugly huh!", 

                           "Sister", 

                           "Gramps - He's deaf, so SHOUT!", 
                           "Bailif - Frequent Visitor" };

JComboBox myComboBox = new JComboBox(familyStrings);

The code above initializes the combo box with an array of strings. This array of strings will be the items displayed in the drop down list.  You can also put icons in a combo box if your object array contains an array of icons instead of strings.

You can specify which item in the drop down list should be selected using code such as:-

myComboBox.setSelectedIndex(0);

Where the argument in the setSelectedIndex is the index number of the item in the array.  You should note that index numbers start from 0.  In this case, the string array familyStrings has 8 items, with index numbers from 0 to 7.  I have set my drop-down list so that the first item in the list (index 0) is initially selected.

 


Editable JComboBox     

Creating an editable combo box is straightforward.  Just create the combo box and then use the setEditable method:-

String[] fontName = { "Times", "Courier", "Arial", "Arial Black"};


JComboBox nameComboBox = new JComboBox(
fontName);

 

//Make the box editable

nameComboBox.setEditable(true);

The applet below shows three combo boxes.  The first one is editable, the other two are not.  You should note that the list remains unchanged when the user enters a value into the combo box.

~~~Applet~~~

 


Other JComboBox Methods 

Here are some methods we can use with the JComboBox class.  You should note that this is not a full list, there are lots of other methods.  You should also check out the inherited JComponent class methods.

  • addItem(Object obj)

  • insertItemAt(Object obj, index i)

Add or insert the specified object into the combo box list.  The insertItemAt method inserts the object at the specified index.

  • Object getSelectedItem()

  • Object getItemAt(index i)

The getSelectedItem method gets the currently selected item from the combo box list.  To retrieve a particular item use the getItemAt method and specify the item's index number.   

  • setEnabled(boolean b)

Enable or disable the combo box using true or false as the argument.  

  • setEditable(boolean b)

Set the combo box to be editable (b is true) or uneditable (b is false). 

  • boolean isEditable()

determine if the combo box is editable.

  • int getItemCount()

Get the number of items in the combo box list. 

  • setSelectedIndex(int index)

Sets the selected item to index

  • removeAllItems()

  • removeItem(index i)

  • removeItemAt(Object obj)

Remove all items from the combo box list or remove a particular item by specifying the item's index number or remove a set of items, specified in the object array. 

 

~Now try the activity~

Activity A

Using appropriate methods from the JComboBox and JComponent class, create an applet with ...

1. An editable JComboBox which lists font names such as "Arial", "Courier", "Times" etc. in alphabetical order.

2. Set the initially selected item in your JComboBox to be  "Times".

 

 

  Site Home 

Java Home   

  Forum  

Course Info

Welcome

Overview

Assessment

Qualification

Scheme of Work

Assignments

Resources

Information

Blackboard

Learning Center

Web Materials

Reading

Java Demos

Utilities

Links

Lecture Materials

Tutorials & Notes

Exercises

Activities

Quizzes

 

Site Home

Top

Unit Home

ADR 2002