0

I am designing a UI in Netbeans and am having problems getting one of my tables to work. The data in it goes not get updated when I call this method:

public void UpdateTable() {
    DefaultTableModel TableModel = new DefaultTableModel();
    //(new Object[] { "ID", "Name", "Age" "Sex" "Address" "blah" }, 5)

    TableView.setModel(TableModel);

    for (Patient k : PatientList) {
        String TableID = String.valueOf(k.getAccountNumber());
        String TableName = k.getName();
        String TableAge = String.valueOf(k.getAge());
        String TableAddress = k.getAddress();
        String TableSex = k.getSex();
        String TablePhone = k.getPhone();

        Object[] Col = { "Name", "Code", "age", "blah", "Who", "ghg" };
        Object[] Data = { TableID, TableName, TableAge, TableAddress,
                TableSex, TablePhone };

        TableModel.addRow(Data, Col); //here
        JTableHeader header = TableView.getTableHeader();

        repaint();
    }

    System.out.println("Model Set");
    TableView.setModel(TableModel);
}

The error is occurring on the line that I commented and I am totally confused as to how to solve this issue.

4

1 回答 1

1

从阅读Java 文档开始

使用创建“空”表模型的方法之一DefaultTableModel是执行类似...

DefaultTableModel model = new DefaultTableModel(
                                    new Object[]{"ID", "Name", "Age" "Sex" "Address" "blah"},
                                    0);

这个很重要。您不希望模型中存在任何现有行。

只有两种addRow方法。

Object[] Data = { TableID, TableName, TableAge, TableAddress, TableSex, TablePhone };
model.addRow(data);

摆脱第一个并且TableView.setModel(TableModel);因为它们没有增加任何价值JTableHeader header = TableView.getTableHeader();repaint();

我强烈建议您阅读如何使用表格以获取更多信息

于 2013-03-14T00:40:57.167 回答