简体   繁体   English

从ArrayList填充jTable

[英]Populate a jTable from an ArrayList

I'm trying to populate a JTable from an ArrayList , The ArrayList is filled with data from my database. 我正在尝试从ArrayList填充JTableArrayList填充了我数据库中的数据。

this is the code I tried : 这是我试过的代码:

DefaultTableModel model = new DefaultTableModel();

        model.setColumnIdentifiers(new String[]{"Numéro d'ordre", "Article", "Quantité", "Remarque"});
        for (gestionstock.LigneBonInterne o : listLigneBonInterne) {
            model.addRow(new String[]{o.getNumOrder().toString(), o.getdesgArt(), o.getQte().toString(), o.getRemarque()});
            System.out.println(o.toString());
        }

        jTable1.setModel(model);

But I get this error message : 但我收到此错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at magasinier.BonInterneDetails.(BonInterneDetails.java:63) magasinier.BonInterneDetails中线程“AWT-EventQueue-0”java.lang.NullPointerException中的异常。(BonInterneDetails.java:63)

the ligne 63 is : jTable1.setModel(model); ligne 63是: jTable1.setModel(model);

I did a test to see if the ArrayList is filled, and I found that the ArrayList is filled with records which means that there is no problem with filling the ArrayList 我做了一个测试,看看是否填充了ArrayList ,我发现ArrayList填充了记录,这意味着填充ArrayList没有问题

How can I solve this problem ? 我怎么解决这个问题 ?

EDIT : 编辑:

I tried to create the JTable using code and assign it to ScrollPane : 我尝试使用代码创建JTable并将其分配给ScrollPane

JTable jTable1 = new JTable(model);
        jTable1.setModel(model);
         jScrollPane1.setViewportView(jTable1);

But I still get the same error this time is the line : jScrollPane1.setViewportView(jTable1); 但是这次我仍然得到相同的错误: jScrollPane1.setViewportView(jTable1);

Initialize the JTable jTable1 prior to setting the TableModel 在设置TableModel之前初始化JTable jTable1

jTable1 = new JTable(model);
jTable1.setModel(model);

在Netbeans调色板中,您可以指定一些自定义的初始化后代码或使用自定义构造函数。

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

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