how yo populate a combo box in java using a loop of an aray of objects

Posted on 2004-12-01
Last Modified: 2008-02-01
how yo populate a combo box in java using a loop of an aray of objects

      else if (e.getSource() instanceof JComboBox)

                      // JComboBox jBox = (JComboBox)e.getSource();
                           //itemSelected= (String) jBox.getSelectedItem();
                           index= jBox.getSelectedIndex();

                           cars[numVideos]=new Car("Dodge","Viper","2003",3112.99);


Question by:faraq1
    1 Comment
    LVL 8

    Accepted Solution

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class ComboBoxDemo extends JPanel {
        JLabel picture;

        public ComboBoxDemo() {
            String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };

            // Create the combo box, select the pig
            JComboBox petList = new JComboBox(petStrings);
            petList.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JComboBox cb = (JComboBox)e.getSource();
                    String petName = (String)cb.getSelectedItem();
                    picture.setIcon(new ImageIcon("images/" + petName + ".gif"));

            // Set up the picture
            picture = new JLabel(new ImageIcon("images/" +
                                       petStrings[petList.getSelectedIndex()] +

            // The preferred size is hard-coded to be the width of the
            // widest image and the height of the tallest image + the border.
            // A real program would compute this.
            picture.setPreferredSize(new Dimension(177, 122+10));

            // Layout the demo
            setLayout(new BorderLayout());
            add(petList, BorderLayout.NORTH);
            add(picture, BorderLayout.SOUTH);

        public static void main(String s[]) {
            JFrame frame = new JFrame("ComboBoxDemo");

            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {System.exit(0);}
            frame.getContentPane().add(new ComboBoxDemo(), BorderLayout.CENTER);

    just follow this links:

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Report events not being triggered 8 67
    sumNumber challenge 16 81
    VBA color chart bars 12 51
    nestparen challenge 4 37
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    A short article about problems I had with the new location API and permissions in Marshmallow
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    746 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

    14 Experts available now in Live!

    Get 1:1 Help Now