我有一个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);
}发布于 2019-11-17 17:51:25
有几种解决办法,包括:
时更新组合框模型。
任何解决方案的细节,包括代码,都将取决于当前程序的详细信息。
附带建议:
initialize()方法中本地声明而不是,因为这将使类的其他成员看不到它,也不应该将任何其他对象分配给需要通过程序更改其状态的局部变量。将变量声明为类的私有实例字段。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());
}
}https://stackoverflow.com/questions/58903312
复制相似问题