java - 为什么这个 JList 不更新以显示 ArrayList 属性

标签 java swing arraylist runtime-error jlist

下面有一个方法,在调用时应该显示列表中 arrayList 元素的属性:

public void updateList(){

    DefaultListModel<String> model = (DefaultListModel<String>)jCustomerList.getModel();

        for(User user: theUserList.Users){        
        model.addElement(user.getName());   

        jCustomerList.setModel(model); 

        }
}

但是,当调用它时,会出现以下错误:

线程“AWT-EventQueue-0”中的异常 java.lang.ClassCastException:supermarketiteration2.ShopJFrame$63 无法转换为 javax.swing.DefaultListModel

如何解决这个问题?

编辑**

模型已全局化,如下所示,但是现在实例化模型时出现错误:

public class ShopJFrame extends javax.swing.JFrame {
private UserList theUserList;
private User currentCustomer;
private Customer castedCustomer;
private SupplierList theSupplierList;
private DeliveryCompanyList theDeliveryCompanyList;
private ProductList theProductList;
private Product selectedProduct;
private Supplier aSupplier;
private DeliveryCompany aDeliveryCompany;
//private JList jCustomerList;

private java.awt.Component jTabInMemory1;
private java.awt.Component jTabInMemory2;
private java.awt.Component jTabInMemory3;
private java.awt.Component jTabInMemory4;
private java.awt.Component jTabInMemory5;
private java.awt.Component jTabInMemory6;

DefaultListModel<String> model;




    public ShopJFrame() throws IOException, FileNotFoundException, ParseException {
        initComponents();
    theUserList = new UserList();
    User currentCustomer = new Customer();
    Customer castedCustomer = null;
    theDeliveryCompanyList = new DeliveryCompanyList();
    aDeliveryCompany = new DeliveryCompany();
    theSupplierList = new SupplierList();
    aSupplier = new Supplier();
    theProductList = new ProductList();

    jTabInMemory1 = jMainTabbedPane.getComponent(1);//PRODUCTS
    jMainTabbedPane.remove(1);
    jTabInMemory2 = jMainTabbedPane.getComponent(1);//REORDER
    jMainTabbedPane.remove(1);
    jTabInMemory3 = jMainTabbedPane.getComponent(1);//SUPPLY CHAIN
    jMainTabbedPane.remove(1);
    jTabInMemory4 = jMainTabbedPane.getComponent(1);//CATALOGUE
    jMainTabbedPane.remove(1);
    jTabInMemory5 = jMainTabbedPane.getComponent(1);//MY ACCOUNT
    jMainTabbedPane.remove(1);
    jTabInMemory6 = jMainTabbedPane.getComponent(1);//MY ACCOUNT
    jMainTabbedPane.remove(1);

    theProductList.loadFromFile(theProductList.getFilename());
    theSupplierList.loadFromFile();
    theDeliveryCompanyList.loadFromFile();
    theUserList.loadFromFile();
    theProductList.displayReorders(jProductReorderTextArea);

    this.updateComboBox("Supplier");
    this.updateComboBox("Delivery Company");
    this.updateComboBox("Products");
    model = (DefaultListModel<String>)jCustomerList.getModel();
    jCustomerList.setModel(model); 

最佳答案

正在 Swing model被传递给 JComponent。

DefaultListModel<String> model = new DefaultListModel<>();
jCustomerList = new JList<String>(model);

model.addElement("Albert Einstein");
...

_(后来的 JavaFX 有可观察数据类型,有点像您想要的绑定(bind)。)_

现在不应创建新的 JList 或 ListModel。

public void updateList() {

    DefaultListModel<String> model = (DefaultListModel<>)jCustomerList.getModel();
    model.addElement("Madame Curie");
}

关于java - 为什么这个 JList 不更新以显示 ArrayList 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60304458/

相关文章:

java - Android:SoundPool/录制音频

java - Android - 使用 HttpURLConnection 将 XML 文件发送到 PHP API 服务器

java - Java中列表的动态列表

java - 如何获得包含从 1 到 "n"的所有数字所需的 ArrayList<Integer> 的最小数量?

java - 在 Java 中管理 Redis 集合的策略

java - 动态数据库连接的连接池

java - Java 初学者编程 - 在对话框 (JOptionPane) 框中使用另一种方法的整数

java - JTextField 清算时的值(value)迷失

java - 如何在 JFrame 中的 JPanel 之间切换

java - 如何将特定数据从 ArrayList<GetterSetter> 对象显示到 ListView?