Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Classic Combo Box

The JComboBox class is the JFC visual component that represents a combo box control. Programming with the JComboBox class is similar to the JList class, presented in the previous section. It contains convenience functions for creating a combo box and setting the data displayed using an array or vector. An example of creating a simple combo box is shown in Listing 10.11, and the resulting component is shown in Figure 10.7.

Listing 10.11 The ComboBoxTest Application

package com.foley.test;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;

import com.foley.utility.ApplicationFrame;

/**
 * An application that displays JComboBox instance
 * in its frame.
 *
 * @author Mike Foley
 **/
public class ComboBoxTest extends Object {

    /**
     * Application entry point.
     * Create a frame, the combo box and display it.
     *
     * @param args Command line parameter. Not used.
     **/
    public static void main( String args[] ) {

        String[] data = {“Apple”, “Orange”, “Grape”, “Banana”,
                        “Mango”, “Pineapple”, “Peach”, “Pear” };
        JFrame frame = new ApplicationFrame( “Combobox Test” );

        JComboBox combobox = new JComboBox( data );
        combobox.setBorder( BorderFactory.createLoweredBevelBorder() );
        frame.getContentPane().add( combobox, BorderLayout.CENTER );

        frame.pack();
        frame.setVisible( true );

    } // main

} // ComboBoxTest


Figure 10.7  JComboBox in use: The first item is selected (top), the user clicks the down-arrow to display choices (middle), and the new item is selected (bottom).

The first combo box, shown in Figure 10.7, depicts when the control is first displayed. The first item from the data array is selected in the combo box. The second combo box appears after the user clicks on the down-arrow to display the possible choices that can be selected. The third combo box shows that an item has been selected.

You may not want to have an element selected when the combo box is initially displayed. This can be accomplished by passing the setSelectedIndex method a parameter of –1.

The window containing the list of items can be shown or hidden programmatically. The methods hidePopup and showPopup are provided for this purpose. The current visibility of the popup can be determined with the isPopupVisible method. The maximum number of rows displayed in the popup window can be controlled using the setMaximumRowCount method. The current value of this property can be queried with the getMaximumRowCount method. The maximumRowCount property is a bound property of the JComboBox class. The list of bound properties introduced by the JComboBox class is presented in Table 10.3.

Table 10.3 Non-Inherited Bound Properties of the JComboBox Class

Property Name Setter Method Getter Method

model setModel getModel
editable setEditable getEditable
maximumRowCount setMaximumRowCount getMaximumRowCount
renderer setRenderer getRenderer
editor setEditor getEditor
enabled setEnabled getEnabled

The pop-up can be specified as a lightweight or heavyweight component by calling the setLightWeightPopupEnabled method. Passing this method true enables lightweight popups. Passing it false enables peer-based popup components. The default value of this property is true, enabling lightweight popups to be used when they will fit in the current window. This is important because if your application mixes lightweight and heavyweight components and a lightweight popup is used, it will be displayed under any heavyweight components in that area. If this is the case, lightweight popups have to be disabled. The isLightWeightPopupEnabled method queries the current state of this property. This is not a bound property of the JComboBox class.

The renderer used by the JComboBox class is an instance of the ListCellRenderer class. This class was described at the beginning of this chapter. The editor property is discussed later in this chapter.

The selected item and its index can be queried with the getSelectedItem and getSelectedIndex methods, respectively. The getSelectedObjects method returns the selected item in an array of length 1, or an empty array if there is no item selected. The selected item can be set programmatically using either the setSelectedItem or setSelectedIndex method. The getItemCount method returns the number of items in the combo box. The getItemAt method returns the item at the specified index. What happens when an index that is out of range is passed to the getItemAt method is unspecified. It is reasonable to expect some type of RuntimeException to be thrown. The DefaultListModel class throws an ArrayIndexOutOfBoundsException exception in this case.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.