Home | | Web Programming | JComboBox - Swing

Chapter: Java The Complete Reference : Introducing GUI Programming with Swing : Exploring Swing

JComboBox - Swing

Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class.

JComboBox

Swing provides a combo box (a combination of a text field and a drop-down list) through the JComboBox class. A combo box normally displays one entry, but it will also display a drop-down list that allows a user to select a different entry. You can also create a combo box that lets the user enter a selection into the text field.

 

In the past, the items in a JComboBox were represented as Object references. However, beginning with JDK 7, JComboBox was made generic and is now declared like this:

 

class JComboBox<E>

 

Here, E represents the type of the items in the combo box.

 

The JComboBox constructor used by the example is shown here: JComboBox(E[ ] items)

Here, items is an array that initializes the combo box. Other constructors are available. JComboBox uses the ComboBoxModel. Mutable combo boxes (those whose entries can

 

be changed) use the MutableComboBoxModel.

 

In addition to passing an array of items to be displayed in the drop-down list, items can be dynamically added to the list of choices via the addItem( ) method, shown here:

 

void addItem(E obj)

 

Here, obj is the object to be added to the combo box. This method must be used only with mutable combo boxes.

 

JComboBox generates an action event when the user selects an item from the list. JComboBox also generates an item event when the state of selection changes, which occurs when an item is selected or deselected. Thus, changing a selection means that two item events will occur: one for the deselected item and another for the selected item. Often, it is sufficient to simply listen for action events, but both event types are available for your use.

One way to obtain the item selected in the list is to call getSelectedItem( ) on the combo box. It is shown here:

 

Object getSelectedItem( )

You will need to cast the returned value into the type of object stored in the list.

 

The following example demonstrates the combo box. The combo box contains entries for "Hourglass", "Analog", "Digital", and "Stopwatch". When a timepiece is selected, an icon-based label is updated to display it. You can see how little code is required to use this powerful component.

 

// Demonstrate JComboBox.

import java.awt.*; import java.awt.event.*; import javax.swing.*;

 

/*

 

<applet code="JComboBoxDemo" width=300 height=200> </applet>

 

*/

 

public class JComboBoxDemo extends JApplet { JLabel jlab;

 

ImageIcon hourglass, analog, digital, stopwatch; JComboBox<String> jcb;

 

String timepieces[] = { "Hourglass", "Analog", "Digital", "Stopwatch" };

 

public void init() { try {

 

SwingUtilities.invokeAndWait( new Runnable() {

 

public void run() { makeGUI();

 

}

 

}

 

);

 

} catch (Exception exc) {

 

System.out.println("Can't create because of " + exc);

 

}

 

}

 

private void makeGUI() {

 

     //Change to flow layout.

     setLayout(new FlowLayout());

 

     //Instantiate a combo box and add it to the content pane.

 

     jcb = new JComboBox<String>(timepieces);

 

add(jcb);

 

     //Handle selections.

 

jcb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) {

 

String s = (String) jcb.getSelectedItem(); jlab.setIcon(new ImageIcon(s + ".png"));

}

 

});

 

 

// Create a label and add it to the content pane.

jlab = new JLabel(new ImageIcon("hourglass.png")); add(jlab);

 

}

 

}

Output from the combo box example is shown here:



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : Introducing GUI Programming with Swing : Exploring Swing : JComboBox - Swing |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.