I need to put the following array into a JComboBox and then store the selected value when the "Submit" button is clicked.
listOfDepartments = new String[5];
listOfDepartments[0] = "Mens Clothing";
listOfDepartments[1] = "Womens Clothing";
listOfDepartments[2] = "Childrens Clothing";
listOfDepartments[3] = "Electronics";
listOfDepartments[4] = "Toys";
//Department: ComboBox that loads from array
// Store values
JButton buttonSubmit = new JButton();
buttonSubmit.setText("Submit");
container.add(buttonSubmit);
buttonSubmit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
//store value from combobox in a variable
}
});
First, create a model...
DefaultComboBoxModel model = new DefaultComboBoxModel(listOfDepartments);
comboBox.setModel(model);
Second, get the selected value when the actionPerformed
event is raised...
String value = (String)comboBox.getSelectedItem();
Take a look at How to Use Combo Boxes for more details.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.