I want to update table when Button is clicked

和自甴很熟 提交于 2019-12-02 04:32:00

Instead of doing this...

String[] colNames = { "BAND", "Test1", "Test2", "Test3",
                    "Test4", "Test5" };
DefaultTableModel model = new DefaultTableModel(data, colNames);
table = new Table(model);

Simply update the existing model

DefaultTableModel model = (DefaultTableModel)table.getModel();
for (Object[] row : data) {
    model.addRow(row);
}

or simply

DefaultTableModel model = (DefaultTableModel)table.getModel();
for (int i = 0; i < length; i++) {
    data = new Object[6];
    data[0] = checkBands[i];
    data[1] = true;
    data[2] = 1;
    data[3] = 2;
    data[4] = 3;
    data[5] = 4;
    model.addRow(data);
}

This assumes that you want to keep adding new rows to the table. You can also use model.setRowCount(0) to clear the table first and then add new rows to it, if that's what you want to.

Swing works a principle of MVC (Model-View-Controller) which separates the view (the JTable) from the data/model (TableModel), this means that the JTable is not bound to the data and can easily be changed or modified by simply changing or modifying the table's model. This is an important concept to understand, as Swing makes a great deal of use of this methodology

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!