Arrays and combo boxes work together by populating the elements of a combo box from an array. An example program demonstrates adding the elements of a string array containing names to a combo box. Each element of the array is assigned to the combo box at its corresponding index position. When an item is selected from the combo box, an item listener displays the selected name in a label.
Original Description:
PRG421
Original Title
PRG421 Discussion Question # 1- Arrays and Combo Boxes
Arrays and combo boxes work together by populating the elements of a combo box from an array. An example program demonstrates adding the elements of a string array containing names to a combo box. Each element of the array is assigned to the combo box at its corresponding index position. When an item is selected from the combo box, an item listener displays the selected name in a label.
Arrays and combo boxes work together by populating the elements of a combo box from an array. An example program demonstrates adding the elements of a string array containing names to a combo box. Each element of the array is assigned to the combo box at its corresponding index position. When an item is selected from the combo box, an item listener displays the selected name in a label.
Write a 200- to 300-word short-answer response for the following
How do arrays and combo boxes work together? Write a small program in which you add the elements of an array to a combo box and post the code for your program. Arrays and combo boxes are works together. Here I used an array of string which has name of persons and I used this array to initialize the elements of combo box. In this example each element of array is assigned to the combo box with the corresponding index position. Like John would be available on the index position 0 of the combo box. Example code: import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class JComboBoxTest extends JFrame {
private JComboBox comboBox;//creating object of JComboBox private JLabel jLabel1, jLabel2; public JComboBoxTest () { super("JComboBox Test"); //set default layout of frame setLayout(new BorderLayout());
jLabel1 = new JLabel("Select Your Name: "); //items for combobox String labels[] = { "John", "George", "Melissa", "Stergios" }; //initializing the combobox object comboBox = new JComboBox(labels);
JPanel panel1 = new JPanel(); //adding items to a panel panel1.add(jLabel1); panel1.add(comboBox); //adding to the in the north of the frame add(panel1, BorderLayout.NORTH);
JPanel panel2 = new JPanel(); jLabel2 = new JLabel(); panel2.add(jLabel2);
add(panel2, BorderLayout.CENTER);
//appening the ItemListener to listen the change on the JComboBox list ItemListener itemListener = new ItemListener() { public void itemStateChanged(ItemEvent itemEvent) { jLabel2.setText("Hello "+ itemEvent.getItem() +"!") ;
}
};
//adding the ItemListener to the combobox comboBox.addItemListener(itemListener);
setSize(300,300);
} public static void main(String[] args) { new JComboBoxTest().setVisible(true); } }