Link to home
Start Free TrialLog in
Avatar of gambit77
gambit77

asked on

swing applet and jcombobox

How would you start awing applet that has jcombo box with at least 10 choices
ASKER CERTIFIED SOLUTION
Avatar of DrWarezz
DrWarezz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of udo_borkowski
udo_borkowski

Here a sample:

=================================
import java.applet.Applet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;

public class ComboBoxApplet extends Applet {
    private JComboBox fComboBox;
    public void init() {
        // create Combobox
        fComboBox = new JComboBox();
        // add items
        fComboBox.addItem("Item 1");
        fComboBox.addItem("Item 2");
        fComboBox.addItem("Item 3");
        fComboBox.addItem("Item 4");
        fComboBox.addItem("Item 5");
        fComboBox.addItem("Item 6");
        fComboBox.addItem("Item 7");
        fComboBox.addItem("Item 8");
        fComboBox.addItem("Item 9");
        fComboBox.addItem("Item 10");
        fComboBox.addItem("Item 11");

        // make the combobox "large" so all items are displayed without
        // scrolling
        fComboBox.setMaximumRowCount(15);

        // add a listener to handle the selection
        fComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onComboboxSelected();
            }
        });

        // add the combobox to the applet
        add(fComboBox);
    }

    protected void onComboboxSelected() {
        // Refer to the currently selected item either by index (<0 when nothing
        // is selected) ...
        int index = fComboBox.getSelectedIndex();
        // or by the item (may be null) ...
        Object selection = fComboBox.getSelectedItem();

        // Do anything you want when an item is selected.
        // (e.g. display a dialog)
        JOptionPane.showMessageDialog(null, "Selected " + index + "\n("
                + selection + ")");
    }
}
=================================

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial