Topics
New to Java
Java Platform Overview | Getting Started | Learning Paths
References & Resources | Certification | Supplements


Building an Application:
Read the API for class JComboBox. Which line of code is correct to make the combo box editable?
The proper way to set a combo box to editable is by calling the setEditable method:
week.setEditable(true);
![]() |
![]() |
JComboBox is just one of a few GUI features you're going to use in this UIWestPanel class, so you must import javax.swing and java.awt packages. The GUI components include some functionality, such as popping up a message, so you also need to implement the ActionListener and ItemListener interfaces.
Next, set up the variables needed for the components. This pane consists of JLabels and JTextFields. Declare variables for each of these.
|
Next, begin the class constructor by setting the layout for this pane to BorderLayout, and setting the background color to white.
|
Now you're ready to work with the individual components for this pane. (You've created text fields for input in previous parts of the Building an Application series, so those are not covered in detail here.) Often applications need components that offer suggested input responses from users. One way of creating such components is to give the user a drop-down menu with a list of items to select, or radio buttons in which to select an item. To create these features, you use the JComboBox and JRadioButton classes.
Combo Boxes
Created with the JComboBox class, a combo box is an object with an editable area (if you desire) and a drop-down list of selectable items. Instances of JComboBox are not editable by default, but can be set to editable with the setEditable method.
To create a JComboBox, call one of the following constructors:
JComboBox(): Creates a default combo box.JComboBox(ComboBoxModel aModel): Creates a combo box that takes its items from an existing ComboBoxModel.JComboBox(Object[] items): Creates a combo box that contains the elements in the specified array.JComboBox(Vector items): Creates a combo box that contains the elements in the specified Vector.The UIWestPanel class calls the JComboBox constructor and supplies a String array to create the list of items.
An array is an object or data structure that holds multiple elements of the same type. Arrays are frequently used in applications that need a grouping of data assigned to one variable. In this case, you'll use the String array to hold the various dive depths the user can choose from the drop-down menu.
You define arrays in several ways. For the combo box you're creating, the easiest technique is to populate the array with the strings to display in the drop-down menu. The following example is an array of strings with the days of the week.
String days[] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
// This array is populated with seven String objects.
// Because they are strings, they must be enclosed in
// quotes.
Once the array is defined, pass it to the JComboBox constructor:
JComboBox week;
String days[] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
week = new JComboBox(days);
After initializing a combo box with an array to provide items for the drop-down menu, you can set how many of those items display in the list at once. If there are more than the specified number, scroll bars appear for the user to view other items on the list. To set maximum rows, call the setMaximumRowCount method and provide an int for the number of items to display:
week.setMaximumRowCount(5);
A JComboBox object uses a ListCellRenderer object to determine how the elements are displayed. In addition, it also uses a ComboBoxModel object to provide methods to access the combo box elements.
When the user explicitly makes a selection, the JComboBox fires an ActionEvent, and the actionPerformed method defines what should happen as a result of this selection.
Another method you'll use is getSelectedItem, which is later called in the actionPerformed method. The getSelectedItem method returns the currently selected item.
|
Note that you can change the number of rows to display (more or less) through the setMaximumRowCount method, if you prefer. In addition, this menu is editable so that the user can change 50 feet to 52, or whatever increment is needed for a particular dive.
Radio buttons appear as small round button icons by default, and can have associated text. They are created in a group so that only one button at a time can be selected. When a button is selected, any other that had been selected is now deselected.
Use one of the following JRadioButton constructors to create radio buttons, depending on your needs:
JRadioButton() JRadioButton(Icon icon) JRadioButton(Icon icon, boolean selected) JRadioButton(String text, Icon icon) JRadioButton(String text, Icon icon, boolean selected) In addition to creating buttons, you must also add them to a button group; otherwise, more than one button can be selected.
Follow these five steps to create a group of radio buttons:
JRadioButton class:
JRadioButton b1 = new JRadioButton("Shark");
JRadioButton b2 = new JRadioButton("Tuna");
JRadioButton b3 = new JRadioButton("Dolphin");
ItemListener:
b1.addItemListener(this); b2.addItemListener(this); b3.addItemListener(this);
ButtonGroup object:
ButtonGroup bg = new ButtonGroup();
bg.add(b1); bg.add(b3); bg.add(b3);
JPanel p = new JPanel(); p.add(b1); p.add(b2); p.add(b3);
When a radio button is selected, an ItemEvent is generated and the itemStateChanged method is called. Within this method, you provide the code for what you want to happen when a radio button is selected. In the case of the Log Dives pane, you want to know which button was selected by getting the name of the button. To get the originator of an event, call the getItemSelectable method.
|
Next, create two buttons: one for the panel that allows a user to save dive information, and another to read saved dive data. For now these buttons are generic. Later, you'll assign text and functionality for these buttons in a different class. Recall that UIWestPanel concentrates on GUI components, not the functionality.
Once you've created the labels, buttons, and radio buttons you need, create a panel to add them to and add the panel to the pane itself. This completes the constructor for the UIWestPanel class.
|
Because the UIWestPanel class builds the GUI features, copy the code for the text area and shark image to a panel and into this constructor. The panel with these objects can then be added to the East area of the pane.
|
|
| B. |
public void actionPerformed(ActionEvent ae) public void itemStateChanged(ItemEvent ie) |
| C. | All of the above |