swing applet and jcombobox

How would you start awing applet that has jcombo box with at least 10 choices
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


check out this tutorial on using Applets: http://java.sun.com/applets/
and this tutorial for creating JComboBoxes: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JComboBox.html

If you're new to Java, then I recommend taking a decent tutorial on it, to learn how to use it:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

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

        // add the combobox to the applet

    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 + ")");

Note that you may also use the sample code when deriving from JApplet.

Just change to:

>> public class ComboBoxApplet extends Applet {


-- public class ComboBoxApplet extends javax.swing.JApplet {

Instead of

>> add(fComboBox);

use either

-- getContentPane().add(fComboBox);

or better

--            javax.swing.JPanel panel = new javax.swing.JPanel();
--            panel.add(fComboBox);
--            getContentPane().add(panel);

if you want to have the Combobox displayed in its "preferred size".

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.