【问题标题】:Why doesn't this JList update to show ArrayList attributes为什么此 JList 不更新以显示 ArrayList 属性
【发布时间】:2020-02-19 16:02:17
【问题描述】:

我有一个方法,它应该在调用时在列表中显示 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); 

【问题讨论】:

  • list.toArray() 创建原始列表的副本 - 您显示的代码中没有绑定。
  • 哦,绑定的语法是什么?
  • 请看编辑

标签: java swing arraylist runtime-error jlist


【解决方案1】:

在 swing 中,model 被传递给 JComponent。

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

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

_(后来的JavaFX有observable数据类型,有点像你想要的binding。)_

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

public void updateList() {

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

【讨论】:

  • 感谢您的帮助,请查看我的编辑,因为它仍显示默认列表。
  • 扩展答案:您的 updateList 永远不会分配给传递给其调用的变量。在java中f(x)永远不会改变变量x。那么 JList 已经存在并且在其 JFrame 中布局,因此不应重新创建并添加到其父级。模型也是如此。
  • 我收到以下错误:线程“AWT-EventQueue-0”中的异常 java.lang.ClassCastException:supermarketiteration2.ShopJFrame$63 无法转换为 javax.swing.DefaultListModel ------ -------------------------------------------------- - - - - - - - - - - - - - - - - - - - - 在线上 - - - - ---------------------------------- DefaultListModel model = (DefaultListModel)jCustomerList.getModel( );
  • 这个 JList 必须首先有一个 DefaultListModel 集。也许更好地创建一个全局model 字段,将其设置在 JList 中,然后立即更改该字段。model
  • 我很抱歉,但我已经把它变成了全局的,它仍然给出同样的错误。我将在编辑后的问题中显示。
猜你喜欢
  • 1970-01-01
  • 2012-07-02
  • 2012-05-08
  • 2011-10-12
  • 2015-02-07
  • 1970-01-01
  • 2020-06-02
  • 2021-05-03
  • 2021-07-06
相关资源
最近更新 更多