返回

java-JTable在添加新行时不会更新

发布时间:2022-09-06 17:58:26 368
# php
 
我无法更新JTable从这里读取的数据ArrayList.当在构造函数中使用相同的代码时,它正常工作,但在addRows无效方法!
public class InvoicesTable extends JPanel {
    private static final String[] cols = {"No.", "Date", "Customer", "Total"} ;
    private static Object[][]data ;

    private static   DefaultTableModel invoicesTableModel;
    private static   JTable invoicesTable;

    public InvoicesTable() {
        invoicesTableModel = new DefaultTableModel(cols,0);
        invoicesTable = new JTable(invoicesTableModel);
        this.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(), "Invoices Table", TitledBorder.LEFT,
                TitledBorder.TOP));
        add(new JScrollPane(invoicesTable));
    }

    public void addRows(ArrayList readInvoiceHeader) {
        System.out.println(readInvoiceHeader.size());
        invoicesTableModel.setRowCount(0);

        for (int i = 0; i < readInvoiceHeader.size(); i++) {
            int invoiceNumber = readInvoiceHeader.get(i).getInvoiceNumber();
            String invoiceDate = readInvoiceHeader.get(i).getInvoiceDate();
            String invoiceCustomer = readInvoiceHeader.get(i).getCustomerName();
            double invoiceTotal = readInvoiceHeader.get(i).getInvoiceTotal();

            Object[] objs = {invoiceNumber, invoiceDate, invoiceCustomer, invoiceTotal};

            invoicesTableModel.addRow(objs);
        }
        invoicesTable.setModel(invoicesTableModel);
    }
}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像