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.
JComboBox( ) - creates a new combo box
JComboBox(Object[ ] array) - creates a new combo box that contains elements in the specified array
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);
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.