Java 更新阵列后更新JcomboBox
我有一个JComboBox,它在我的主类中显示数组的内容,但我有另一个类,它有一个函数,可以根据用户输入更改数组。但是,即使数组在主类中已经更新,JComboBox也不会更新(我使用打印来检查它是否确实更新)。当更多项添加到数组或从数组中删除时,JComboBox是否有更新的方法 这是主类中的JComboBox,其中Java 更新阵列后更新JcomboBox,java,swing,jcombobox,Java,Swing,Jcombobox,我有一个JComboBox,它在我的主类中显示数组的内容,但我有另一个类,它有一个函数,可以根据用户输入更改数组。但是,即使数组在主类中已经更新,JComboBox也不会更新(我使用打印来检查它是否确实更新)。当更多项添加到数组或从数组中删除时,JComboBox是否有更新的方法 这是主类中的JComboBox,其中buildingNames是存储信息的数组,将被更新 private String[] buildingNames; public mainWindow() { initi
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);
}
存在几种解决方案,包括:
- 当数组更新时,使用观察者模式通知相关对象,然后为组合框创建新模型,并在发生更新时将其加载到组合框中。这将是一个更大的模型-视图-控制器程序结构的一部分,可能是我要做的
- 创建您自己的模型类,扩展抽象组合框模型类,一个使用数组本身的模型类,一个在数组更改时再次收到通知的模型类
- 完全去掉数组,在需要的时候和地方更新组合框模型
- 组合框变量不应在
方法中本地声明,因为这将使它对类的其余部分不可见,也不应将任何其他对象指定给需要程序更改其状态的局部变量。将变量声明为类的私有实例字段initialize()
- 不要设置组件的边界或使用空布局,而是设置属性(可见行数、原型显示值…),并允许组件自行调整大小
- 如果您的数组内容在程序ru期间可能会发生很大变化,那么您可能应该使用一个集合,例如自定义构建类的
,或者更好的是使用ArrayList
ArrayList
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.awt.event.ActionListener;
导入java.awt.event.KeyEvent;
导入javax.swing.*;
@抑制警告(“串行”)
公共类BuildingTest扩展了JPanel{
//用于保存所有建筑名称字符串的模型
私有DefaultComboxModel ComboxModel=新的DefaultComboxModel(新字符串[]{“Foo”、“Bar”、“Baz”});
private JComboBox selectBuildingCombo=新建JComboBox(comboModel);
//允许用户向模型添加更多字符串的文本字段
私有JTextField entryField=新JTextField(10);
私有JButton enterBuildingBtn=新JButton(“输入建筑名称”);
公共建筑测试(){
//组合框的大小更大
选择buildingCombo.setPrototypeDisplayValue(“abcdefghijklmnopqrstuv”);
添加(选择BuildingCombo);
添加(新JLabel(“输入新建筑名称:”);
添加(入口字段);
添加(输入buildingbtn);
选择BuildingCombo.addActionListener(e->{
字符串选择=(字符串)selectBuildingCombo.getSelectedItem();
如果(选择!=null){
System.out.println(“所选项目:+所选内容”);
}
});
//当用户希望添加到组合模型时发生
ActionListener enterBuildingListener=e->{
//从文本字段获取文本
String text=entryField.getText().trim();
如果(!text.isEmpty()){
//如果不是空的,则添加到模型
comboModel.addElement(文本);
entryField.selectAll();
}
};
//将此操作侦听器添加到文本字段和按钮
enterBuildingBtn.addActionListener(enterBuildingListener);
addActionListener(enterBuildingListener);
输入buildingbtn.set助记符(KeyEvent.VK_E);
}
私有静态void createAndShowGui(){
BuildingTest主面板=新建BuildingTest();
JFrame=新JFrame(“建筑测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(主面板);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
调用器(()->createAndShowGui());
}
}
非常感谢您的回答和建议!我已经尝试了你的建议,效果很好!:)再次感谢你!