• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 694
  • Last Modified:

swing applet and jcombobox

How would you start awing applet that has jcombo box with at least 10 choices
0
gambit77
Asked:
gambit77
  • 2
2 Solutions
 
DrWarezzCommented:
Hey,

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:
http://java.sun.com/docs/books/tutorial/

gL,
[r.D]
0
 
udo_borkowskiCommented:
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 + ")");
    }
}
=================================

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

Just change to:

>> public class ComboBoxApplet extends Applet {

to

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


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now