【发布时间】:2019-11-17 17:41:58
【问题描述】:
我有一个 JComboBox,它在我的主类中显示一个数组的内容,但我有另一个类,它具有根据用户输入更改数组的函数。然而,即使数组已在主类中更新,JComboBox 也不会更新(我使用打印来检查它是否确实更新了)。有没有办法让 JComboBox 在向数组中添加更多项或从数组中删除项时更新?
这是主类中的JComboBox,其中buildingNames是存储信息的数组,将被更新。
private String[] buildingNames;
public mainWindow() {
initialize();
}
private void initialize() {
frame = new JFrame("Main Window");
frame.setBounds(0, 0, 1280, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setBackground(Color.WHITE);
frame.setResizable(false);
buildingNames = {"Costlemark","Science","Research"} //This will get updated
DefaultComboBoxModel BuildingModel = new DefaultComboBoxModel(buildingNames);
JComboBox selectBuilding = new JComboBox(BuildingModel);
selectBuilding.setBounds(46, 82, 150, 40);
frame.getContentPane().add(selectBuilding);
}
【问题讨论】: