gpt4 book ai didi

java - 如何让JComboBox自动更新?

转载 作者:行者123 更新时间:2023-12-02 00:23:12 25 4
gpt4 key购买 nike

我正在努力更新 JComboBox 中的项目。当我从文件加载项目时,组合框显示正确,但是当我尝试从组合框项目添加或删除项目时,组合框不会自动更新,它仍然保留相同的项目。这是我的代码这是我加载组合框项目的位置

ObjectInputStream input;
try {
// TODO add your handling code here:
JFileChooser openFileChooser = new JFileChooser();
openFileChooser.setCurrentDirectory(new File("."));
if (openFileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
input = new ObjectInputStream(new FileInputStream(openFileChooser.getSelectedFile()));
diary = (Diary)input.readObject();
jTextArea3.setText(diary.getUnitCollection().toString());
input.close();
//Load Unit Item
for (Unit u: diary.getUnitCollection()){
jComboBox8.addItem(u.getUnitName());
jComboBox1.addItem(u.getUnitName());
}
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}

这是删除项目的按钮

 private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
for (int i = 0; i < diary.getUnitCollection().size(); i++){
if (jComboBox8.getSelectedItem().equals(diary.getUnitCollection().get(i).getUnitName())){
diary.getUnitCollection().remove(diary.getUnitCollection().get(i));
jTextArea3.setText("The Unit " + jComboBox8.getSelectedItem()+ " has been removed successfully");
}
}
}

编辑:只需修复样式(代码块)

最佳答案

您需要通过模型添加和删除内容,请查看http://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultComboBoxModel.html

关于java - 如何让JComboBox自动更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10567871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com