Jtable not updating with my abstracttablemodel

血红的双手。 提交于 2019-11-29 12:58:25

This is where the issue is value = obj;

In setValueAt method you are not setting the values to the respective obj value's. The way you are accessing the getValueAt similarly set the obtained value to the respective array position.

Use ArrayList instead of using arrays. You can easily access all the methods.

class TableData {       
    private String name;
    private String grade;
    private String subject;
    private String staff;
   // Add getters and setters.
}

This is an example of the TableModel using ArrayList.

class AllTableModel extends AbstractTableModel {

    List<TableData> tableData = new ArrayList<TableData>();

    Object[] columnNames = {"Name", "Grade", "Subject", "Staff"};

    public AllTableModel(List<TableData> data) {

        tableData = data;
    }

    public List<TableData> getTableData() {
        return tableData;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column].toString();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        return tableData.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        TableData data = tableData.get(rowIndex);
        switch (columnIndex) {
        case 0:
            return data.getName();
        case 1:
            return data.getGrade();
        case 2:
            return data.getSubject();
        case 3:
            return data.getStaff();
        default:
            return null;
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        TableData data = tableData.get(rowIndex);
        switch (columnIndex) {
        case 0:
            data.setName(aValue == null ? null : aValue.toString());
        case 1:
            data.setGrade(aValue == null ? null : aValue.toString());
        case 2:
            data.setSubject(aValue == null ? null : aValue.toString());
        case 3:
            data.setStaff(aValue == null ? null : aValue.toString());
        }
    }

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