Java,如何引用JTable [英] Java, how to refres JTable

查看:93
本文介绍了Java,如何引用JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个JTable。一个用于发票清单,另一个用于发票产品清单。我创建了一个侦听器,以便可以通过单击鼠标首先从JTable中获取发票的ID值,然后使用另一种方法为其他表生成新的表内容(方法工作正常)。但是,在更新表之后,我试图找到一种方法来刷新它,但是没有成功...我只是在寻找一种方法来刷新第二个表,当我触发鼠标事件时

I have two JTables. One is for the list of invoices and the other is for the list of products in invoices. I have created a listener so that I can get id value of invoices from first JTable with a mouse click, then using another method I generate a new table content for other table (method works fine). However, after updating table, I have tried to find ways to refresh it, unsuccessfully... I'm just looking for a way to refresh the second table the moment I trigger the mouse event

下面是代码的一部分:

带有两个JTable的面板

Panel with two JTables

public JPanel tabInvoices() {

    final JPanel panel = new JPanel(new MigLayout("", "20 [grow, fill] 10 [grow, fill] 20", "20 [] 10 [] 20"));

    final DefaultTableModel model1;
    final JTable table1, table2;

    String data[][] = {};
    String col1[] = {"ID", "Date"};
    String col2[] = {"ID", "Name", "Type", "Price", "Quantity"};
    Object[][] selrowData = {};   

    /** Labels and buttons **/
    JLabel labelInv = new JLabel("List of all invoices");
    JLabel labelPro = new JLabel("List of all products in this invoice");

    /** TABLE: Invoices **/
    model1 = new DefaultTableModel(data, col1);
    invoiceInfo = new DefaultTableModel(selrowData,col2);

    /** Load the invoices from DB **/
    List<Invoice> listInv = is.getAllInvoices();
    for (int i = 0; i < listInv.size(); i++) {
        model1.insertRow(i, new Object[] {
                listInv.get(i).getID(),
                listInv.get(i).getDate()
        });
    }

    /** TABLE: Invoices **/
    table1 = new JTable(model1){
        public boolean isCellEditable(int r, int c) {
            return false;
        }
    };

    /** TABLE: Invoice Info **/
    table2 = new JTable(invoiceInfo){
        public boolean isCellEditable(int r, int c) {
            return false;
        }
    };


    /** Cell and Row selection listener **/
    ListSelectionModel cellSelectionModel = table1.getSelectionModel();
    cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent arg0) {
            int id = (int)table1.getValueAt(row, 0);            // Get ID from first table
            invoiceInfo.getDataVector().removeAllElements();    // Remove everything from second table
            invoiceInfo = getInvoiceInfo(id, invoiceInfo);      // replace with new values

            /**
             * HERE'S WHERE I NEED TO REFRESH THE TABLE2. HOW?
             */
        }
    });


    /** Load the products from DB belonging to this invoice **/
    List<Product> listPro = is.getInvoiceInfo(row); // TODO Listener!
    for (int i = 0; i < listPro.size(); i++) {
        invoiceInfo.insertRow(i, new Object[] {
                listPro.get(i).getID(),
                listPro.get(i).getName(),
                listPro.get(i).getType(),
                listPro.get(i).getPrice(),
                listPro.get(i).getQuantity()
        });
    }
    /** Scroll Panes **/
    JScrollPane scrollInv = new JScrollPane(table1);
    JScrollPane scrollPro = new JScrollPane(table2);

    /** Add everything to the panel **/
    panel.add(labelInv);
    panel.add(labelPro, "wrap");
    panel.add(scrollInv);
    panel.add(scrollPro);

    return panel;
}

该方法用于每次我单击其中的任何单元格来重新创建第二个表左表:

And method used to recreate the second table each time I click on any cell in left table:

public DefaultTableModel getInvoiceInfo(int id, DefaultTableModel model) {

    String data1[][] = {};
    String col1[] = {"ID", "Name", "Type", "Price", "Quantity"};
    DefaultTableModel info = new DefaultTableModel(data1,col1);

    /** Load products from DB **/
    List<Product> products = is.getInvoiceInfo(id);
    for (int i = 0; i < products.size(); i++) {
        info.insertRow(i, new Object[] {
                products.get(i).getID(),
                products.get(i).getName(),
                products.get(i).getType(),
                products.get(i).getPrice(),
                products.get(i).getQuantity()
        });
    }
    return info;
}


推荐答案

不需要显式代码即可刷新您的表。 insertRow()为您触发一个change事件,该表将侦听该事件并根据需要重新绘制自身。

No explicit code is required to refresh your table. insertRow() fires a change event for you, which the table listens to and repaints itself as necessary.

但是,您正在getInvoiceInfo方法中创建DefaultTableModel的新实例。 。您的JTable不会监听这个新模型。因此,通过insertRow()进行的更新将变为稀薄状态,并且您的JTable始终没有对其的引用。

However, you're creating a new instance of DefaultTableModel in getInvoiceInfo method. This new model is not listened to by your JTable. Therefore updates via insertRow() fire changes into "thin air", and your JTable doesn't have a reference to it anyway.

一个快速解决方案是将代码更改为以下内容:

A quick fix would be to change the code to the following:

public void getInvoiceInfo(int id, DefaultTableModel model) {

    /** Load products from DB **/
    List<Product> products = is.getInvoiceInfo(id);
    for (int i = 0; i < products.size(); i++) {
        model.insertRow(i, new Object[] {
                products.get(i).getID(),
                products.get(i).getName(),
                products.get(i).getType(),
                products.get(i).getPrice(),
                products.get(i).getQuantity()
        });
    }
}

这篇关于Java,如何引用JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆