更新数组后更新 JcomboBox
update JcomboBox after Array is updated
我有一个 JComboBox,它在我的主 class 中显示数组的内容,但我有另一个 class,它具有根据用户输入更改数组的功能。但是 JComboBox 不会更新,即使数组已在主 class 中更新(我使用打印来检查它是否确实更新)。有没有办法让 JComboBox 在数组中添加更多项或从数组中删除项时进行更新?
这是主体class中的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);
}
存在多种解决方案,包括:
- 使用观察者模式在数组更新时通知相关对象,然后为组合框创建一个新模型,并在更新发生时将其加载到组合中。这将是更大的模型-视图-控制器程序结构的一部分,并且可能是我要走的路。
- 创建您自己的模型 class,扩展抽象组合框模型 class,一种使用数组本身,另一种在数组更改时再次收到通知。
- 完全摆脱数组,而是在需要的时候和需要的地方更新组合框模型
任何解决方案的细节(包括代码)将取决于您当前程序的细节。
侧面建议:
- 您的组合框变量应该而不是在
initialize()
方法中本地声明,因为这将使它对class 的其余部分,也不应将任何其他对象分配给需要由程序更改其状态的局部变量。将变量声明为 class. 的私有实例字段
- 永远不要设置组件的边界或使用空布局,而是设置属性(可见行数、原型显示值...)并允许组件自行调整大小。
- 如果您的数组内容在程序 ru 期间可能会发生很大变化,那么您可能应该使用一个集合,例如
ArrayList<String>
,或者更好的是,您的 ArrayList<Building>
自定义建筑 class.
关于我们只使用组合框模型的最后一个建议示例:
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());
}
}
我有一个 JComboBox,它在我的主 class 中显示数组的内容,但我有另一个 class,它具有根据用户输入更改数组的功能。但是 JComboBox 不会更新,即使数组已在主 class 中更新(我使用打印来检查它是否确实更新)。有没有办法让 JComboBox 在数组中添加更多项或从数组中删除项时进行更新?
这是主体class中的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);
}
存在多种解决方案,包括:
- 使用观察者模式在数组更新时通知相关对象,然后为组合框创建一个新模型,并在更新发生时将其加载到组合中。这将是更大的模型-视图-控制器程序结构的一部分,并且可能是我要走的路。
- 创建您自己的模型 class,扩展抽象组合框模型 class,一种使用数组本身,另一种在数组更改时再次收到通知。
- 完全摆脱数组,而是在需要的时候和需要的地方更新组合框模型
任何解决方案的细节(包括代码)将取决于您当前程序的细节。
侧面建议:
- 您的组合框变量应该而不是在
initialize()
方法中本地声明,因为这将使它对class 的其余部分,也不应将任何其他对象分配给需要由程序更改其状态的局部变量。将变量声明为 class. 的私有实例字段
- 永远不要设置组件的边界或使用空布局,而是设置属性(可见行数、原型显示值...)并允许组件自行调整大小。
- 如果您的数组内容在程序 ru 期间可能会发生很大变化,那么您可能应该使用一个集合,例如
ArrayList<String>
,或者更好的是,您的ArrayList<Building>
自定义建筑 class.
关于我们只使用组合框模型的最后一个建议示例:
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());
}
}