简体   繁体   English

JTable在插入删除或更新数据后如何刷新表模型。

[英]JTable How to refresh table model after insert delete or update the data.

This is my jTable 这是我的jTable

private JTable getJTable() {
    String[] colName = { "Name", "Email", "Contact No. 1", "Contact No. 2",
            "Group", "" };
    if (jTable == null) {
        jTable = new JTable() {
            public boolean isCellEditable(int nRow, int nCol) {
                return false;
            }
        };
    }
    DefaultTableModel contactTableModel = (DefaultTableModel) jTable
            .getModel();
    contactTableModel.setColumnIdentifiers(colName);
    jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    return jTable;
}

I will call this method to retrieve the data from database and put it into table model 我将调用此方法从数据库中检索数据并将其放入表模型中

public void setUpTableData() {
    DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
    ArrayList<Contact> list = new ArrayList<Contact>();
    if (!con.equals(""))
        list = sql.getContactListsByGroup(con);
    else
        list = sql.getContactLists();
    for (int i = 0; i < list.size(); i++) {
        String[] data = new String[7];

            data[0] = list.get(i).getName();
            data[1] = list.get(i).getEmail();
            data[2] = list.get(i).getPhone1();
            data[3] = list.get(i).getPhone2();
            data[4] = list.get(i).getGroup();
            data[5] = list.get(i).getId();

        tableModel.addRow(data);
    }
    jTable.setModel(tableModel);
}

Currently I was using this method to refresh the table after updating the table data. 当前,我在更新表数据后使用此方法刷新表。 I will first clear the table 我先清理桌子

DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
tableModel.setRowCount(0);

and then restructure the table model again so it will refresh the jTable. 然后再次重组表模型,以便刷新jTable。 But I was thinking is there any best practices or better way to do that? 但是我在想是否有最佳实践或更好的方法?

If you want to notify your JTable about changes of your data, use 如果您想将数据更改通知JTable ,请使用
tableModel.fireTableDataChanged()

From the documentation : 文档中

Notifies all listeners that all cell values in the table's rows may have changed. 通知所有侦听器表行中的所有单元格值可能已更改。 The number of rows may also have changed and the JTable should redraw the table from scratch. 行数也可能已更改,JTable应该从头开始重新绘制表。 The structure of the table (as in the order of the columns) is assumed to be the same. 假定表的结构(按列的顺序)是相同的。

The faster way for your case is: 处理您案件的更快方法是:

    jTable.repaint(); // Repaint all the component (all Cells).

The optimized way when one or few cell change: 一个或几个单元格更改时的优化方式:

    ((AbstractTableModel) jTable.getModel()).fireTableCellUpdated(x, 0); // Repaint one cell.

try this 尝试这个

public void setUpTableData() {
    DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();

    /**
    * additional code.
    **/
    tableModel.setRowCount(0);
    /**/
    ArrayList<Contact> list = new ArrayList<Contact>();
    if (!con.equals(""))
        list = sql.getContactListsByGroup(con);
    else
        list = sql.getContactLists();
    for (int i = 0; i < list.size(); i++) {
        String[] data = new String[7];

        data[0] = list.get(i).getName();
        data[1] = list.get(i).getEmail();
        data[2] = list.get(i).getPhone1();
        data[3] = list.get(i).getPhone2();
        data[4] = list.get(i).getGroup();
        data[5] = list.get(i).getId();

        tableModel.addRow(data);
    }
    jTable.setModel(tableModel);
    /**
    * additional code.
    **/
    tableModel.fireTableDataChanged();
    /**/
}
DefaultTableModel dm = (DefaultTableModel)table.getModel();
dm.fireTableDataChanged(); // notifies the JTable that the model has changed

使用会导致表更新的java.util.Observablejava.util.Observer会更好吗?

I did it like this in my Jtable its autorefreshing after 300 ms; 我在Jtable中做到了这一点,它在300毫秒后自动刷新;

DefaultTableModel tableModel = new DefaultTableModel(){
public boolean isCellEditable(int nRow, int nCol) {
                return false;
            }
};
JTable table = new JTable();

Timer t = new Timer(300, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addColumns();
                remakeData(set);
                table.setModel(model);
            }
        });
        t.start();

private void addColumns() {
        model.setColumnCount(0);
        model.addColumn("NAME");
            model.addColumn("EMAIL");} 

 private void remakeData(CollectionType< Objects > name) {
    model.setRowCount(0);
    for (CollectionType Objects : name){
    String n = Object.getName();
    String e = Object.getEmail();
    model.insertRow(model.getRowCount(),new Object[] { n,e });
    }}

I doubt it will do good with large number of objects like over 500, only other way is to implement TableModelListener in your class, but i did not understand how to use it well. 我怀疑它将对超过500个的大量对象是否有用,只有其他方法才能在您的类中实现TableModelListener,但我不知道如何很好地使用它。 look at http://download.oracle.com/javase/tutorial/uiswing/components/table.html#modelchange 看看http://download.oracle.com/javase/tutorial/uiswing/components/table.html#modelchange

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

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