how to populate a combo using an array of objects, urgent response pls

private void setupComboBox()
 {
               //Declare the variables
                    jBox = new JComboBox();
               String[] cars={"Dodge  Viper  2003   $72,471, Chevrolet   Suburban   1998   $20,885"};
                    for (int i=0; i < numVideos; i++)
                    {
                             jBox.addItem(cars[i].getModel());
                    }

               jBox.setBounds(10,10,200,25);

        }
 
faraq1Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
private void setupComboBox()
 {
               //Declare the variables
                   jBox = new JComboBox();
               String[] cars={"Dodge  Viper  2003   $72,471", "Chevrolet   Suburban   1998   $20,885"};
                   for (int i=0; i < cars.length; i++)
                   {
                           jBox.addItem(cars[i]);
                   }

               jBox.setBounds(10,10,200,25);

        }
0
 
petmagdyCommented:
like this:

private void setupComboBox()
 {
               //Declare the variables
                   jBox = new JComboBox();

 String[] cars={"Dodge Viper  2003   $72,471", "Chevrolet   Suburban   1998   $20,885"};
                   for (int i=0; i < numVideos; i++)
                   {
                           jBox.addItem(cars[i]);
                   }

               jBox.setBounds(10,10,200,25);

        }
 
0
 
fifaloverCommented:
Since the default constructor is going to create an instance of DefaultComboBoxModel anyways, you can create one yourself and give it to JComboBox with the cars already in it.

private void setupComboBox()
{
               //Declare the variables
               String[] cars={"Dodge  Viper  2003   $72,471", "Chevrolet   Suburban   1998   $20,885"};
               jBox = new JComboBox(new DefaultComboBoxModel(cars));
               
               jBox.setBounds(10,10,200,25);
}
0
All Courses

From novice to tech pro — start learning today.