简体   繁体   English

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

[英]Why doesn't this JList update to show ArrayList attributes

I have below a method that should display attributes of arrayList elements in a list when called:我有一个方法,它应该在调用时在列表中显示 arrayList 元素的属性:

public void updateList(){

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

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

        jCustomerList.setModel(model); 

        }
}

however when it is called the following error appears :但是当它被调用时会出现以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: supermarketiteration2.ShopJFrame$63 cannot be cast to javax.swing.DefaultListModel线程“AWT-EventQueue-0”中的异常 java.lang.ClassCastException:superiteration2.ShopJFrame$63 不能转换为 javax.swing.DefaultListModel

How do I resolve this?我该如何解决?

EDIT**编辑**

The model has been made global as shown below, however now the error occurs when instantiating the model:该模型已被设为全局,如下所示,但是现在实例化模型时发生错误:

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); 

In swing a model is passed to the JComponent.在 Swing 中, 模型被传递给 JComponent。

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

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

_(The later JavaFX has observable data types, a bit more like binding as you intended.)_ _(后来的 JavaFX 有可观察的数据类型,有点像你想要的绑定。)_

Now one should neither create a new JList or ListModel.现在既不应该创建一个新的 JList 或 ListModel。

public void updateList() {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM