Return to Unit Menu | | | Terms of Use  |

Creating a JComboBox

In Swing, the JComboBox allows the user to choose from a designated list of options in the form of a "drop-down" box. The choice selected by the user will appear on the top of the menu.



reddot JComboBox( ) - creates a new combo box
reddot JComboBox(Object[ ] array)
- creates a new combo box that contains elements in the specified array

beware Starting with Java 7, JComboBox falls under the Warning: "JComboBox is a raw type. References to generic type JComboBox<E> should be parameterized."
This means that you must now describe the type of data that the Combo Box is holding.

The following type of adjustment will remove this warning:
JComboBox<String> box = new JComboBox<>( );


JComboBox<String> box;
String [ ] subjects = {"Math", "Science","English","History","Genetics"};
box = new JCombo<>(subjects);


Create and Activate a JComboBox:

Example: The user is given a drop-down box showing the choice of five subjects, and asked to choose his/her favorite subject. When the Choose button is clicked, the message will appear stating which subject was chosen.



Return to Unit Menu | | | Terms of Use  |

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area,
Help us keep these resources free. Thank you.