swing applet and  jcombobox

Posted on 2004-11-27
Last Modified: 2013-11-23
How would you start awing applet that has jcombo box with at least 10 choices
Question by:gambit77
    LVL 9

    Accepted Solution


    check out this tutorial on using Applets:
    and this tutorial for creating JComboBoxes:

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

    LVL 3

    Expert Comment

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

    LVL 3

    Assisted Solution

    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".


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now