我有一个 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);
}
最佳答案
存在多种解决方案,包括:
- 使用观察者模式在数组更新时通知相关对象,然后为组合框创建一个新模型,并在发生更新时将其加载到组合中。这将是更大的模型- View - Controller 程序结构的一部分,并且可能是我会采用的方式。
- 创建您自己的模型类,扩展抽象组合框模型类,该模型类使用数组本身,并且在数组更改时再次收到通知。
- 完全摆脱数组,而是在需要的时间和地点更新组合框模型
任何解决方案的详细信息(包括代码)都取决于您当前程序的详细信息。
侧面建议:
- 您的组合框变量不应在
initialize()
中本地声明。方法,因为这将使其对类的其余部分不可见,也不应将任何其他对象分配给需要由程序更改其状态的局部变量。将变量声明为类的私有(private)实例字段。 - 切勿设置组件的边界或使用空布局,而是设置属性(可见行数、原型(prototype)显示值...)并允许组件自行调整大小。
- 如果您的数组内容可能会在程序 ru 期间发生很大变化,那么您可能应该使用诸如
ArrayList<String>
之类的集合。 ,或者更好的是ArrayList<Building>
您的自定义建筑类别。
有关我们仅使用组合框模型的最后一个建议的示例:
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class BuildingTest extends JPanel {
// model to hold all the building name strings
private DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel<>(new String[] {"Foo", "Bar", "Baz"});
private JComboBox<String> selectBuildingCombo = new JComboBox<>(comboModel);
// text field to allow user to add more strings to model
private JTextField entryField = new JTextField(10);
private JButton enterBuildingBtn = new JButton("Enter Building Name");
public BuildingTest() {
// the size of the combobox larger
selectBuildingCombo.setPrototypeDisplayValue("abcdefghijklmnopqrstuv");
add(selectBuildingCombo);
add(new JLabel("Enter new building name:"));
add(entryField);
add(enterBuildingBtn);
selectBuildingCombo.addActionListener(e -> {
String selection = (String) selectBuildingCombo.getSelectedItem();
if (selection != null) {
System.out.println("Selected Item: " + selection);
}
});
// occurs when user wants to add to combo model
ActionListener enterBuildingListener = e -> {
// get text from text field
String text = entryField.getText().trim();
if (!text.isEmpty()) {
// if not empty, add to model
comboModel.addElement(text);
entryField.selectAll();
}
};
// add this action listener to both the text field and the button
enterBuildingBtn.addActionListener(enterBuildingListener);
entryField.addActionListener(enterBuildingListener);
enterBuildingBtn.setMnemonic(KeyEvent.VK_E);
}
private static void createAndShowGui() {
BuildingTest mainPanel = new BuildingTest();
JFrame frame = new JFrame("Building Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
关于java - 更新数组后更新 JcomboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58903312/