datalogo
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

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.

dividerdash

JComboBox

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<>( );

dividerdash

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

box.setBounds(50,80,90,30);
panel.add(box);

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.

 dropdrop2

dd1
dd2

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

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, JavaMathBits.com.
Help us keep these resources free. Thank you.