JaVa
   

The JComboBox

The JComboBox class is used to create a drop-down menu from which the user can select options. Let's look at an example app so we can see how it is used and also how it looks.

Code Listing 8: Using the JComboBox
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 public class JComboBoxExample extends JFrame implements
 ActionListener
{
 public static void main(String[] argv)
 {
 JComboBoxExample mainApp = new JComboBoxExample();
 }
 public JComboBoxExample()
 {
 super("JComboBox Example");
 setBounds(0, 0, 250, 150);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // Create the label...
 label = new JLabel("You have selected: ");
 label.setLocation(10, 10);
 label.setSize(label.getPreferredSize());
 // Create a combo box...
 String[] options = {"Option 1", "Option 2", "Option 3"};
 combobox = new JComboBox(options);
 combobox.setLocation(10, 40);
 combobox.setSize(combobox.getPreferredSize());
 // Add another two options to the combo box...
 combobox.addItem("Option 4");
 combobox.addItem("Option 5");
 // Add the action listeners
 combobox.addActionListener(this);
 // Add the objects to the content pane...
 getContentPane().add(label);
 getContentPane().add(combobox);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == combobox)
 {
 label.setText("You have selected: " +
 combobox.getSelectedItem());
 label.setSize(label.getPreferredSize());
 }
 }
 JLabel label;
 JComboBox combobox;
}


Java End example

When we run the JComboBox example app, the following can be seen.

Java ScreenShot
Screenshot: The JComboBox example app

Using the combo box object is relatively simple. Let's look more closely at the code now and see how we create it and also react to the user changing the selection. Before creating our JComboBox object, we first create an array of strings to represent the options initially available to the user in the combo box. This can be seen in the following line of code:

String[] options = {"Option 1", "Option 2", "Option 3"};


Once we have defined our initial options, call the constructor with the array as our parameter. This can be seen in the following line of code:

combobox = new JComboBox(options);


Note that it is not essential to specify initial options, as we can add them after the combo box has been created. To add the additional two options after it has been created, we use the addItem method, which can be seen in the following two lines of code:

combobox.addItem("Option 4");
combobox.addItem("Option 5");


For reacting to the user changing the selection, we simply add an action listener to the combobox. This can be seen in the following line of code:

combobox.addActionListener(this);


This is done in exactly the same way as we handle the user clicking on JButton objects. Our main class implements the ActionListener interface, so we have defined the actionPerformed method. We first check that the source of the event has come from our combo box with the following if statement:

if(e.getSource() == combobox)
{
 …
}


Within the if statement, we simply get the current selection from our combo box using the getSelectedItem method from our combobox object. We then use the string; this returns to update the label that we have also created in our app. The code within the if statement is as follows:

label.setText("You have selected: " + combobox.getSelectedItem());
 label.setSize(label.getPreferredSize());


JaVa
   
Comments