0

我是编程新手,正在做我的第一份学校作业。我编写了一个 gui,它在添加到 jpaddedpane 的 jtable 中接受输入和输出数据。当表格首次出现时,它会显示所有正确的数据。但是当我输入新输入时,表格不会更新。我也很肯定问题在于我对 AstractTableModel 的实现。有人可以看看并尽快为我纠正吗?提前致谢。

附言。nh、vh、hNam、proc_1 和 proc_ 分别是整数、字符串、整数、字符串和字符串数组。它们保存要在表中显示的数据。

 public class TableModel extends AbstractTableModel  {
        int numRows;
        String colNames[] = { "NH", "Horse Names", "VH",
                              "Proc. I", "Proc. II" };        
        Object[][] obj;

        TableModel()  {
            super();
            numRows = fnh;
            obj = new Object[fnh][5];

            for( int i = 0; i < fnh; i++ )  {
              for ( int j = 0; j < 5; j++ ) {
                  if ( j == 0 ) 
                      obj[i][0] = (Integer)nh[i];
                  else if ( j == 1 )
                      obj[i][1] = (String)hNam[i];
                  else if ( j == 2 )
                      obj[i][2] = (Integer)vh[i];
                  else if ( j == 3 )
                      obj[i][3] =(String)proc_1[i];
                  else 
                      obj[i][4] =(String)proc_2[i];        
               }
           }
        }

        @Override
        public int getRowCount()  {
           return numRows;
        }

        @Override
        public int getColumnCount()  {
            return 5;
        }

        @Override
        public String getColumnName( int c ) {
            return colNames[c];
        }

        @Override
        public Object getValueAt( int r, int c )  {
            if ( c == 0 ) 
                return nh[r];
            else if ( c == 1 )
                return hNam[r];
            else if ( c == 2 )
                return vh[r] ;
            else if ( c == 3 )
                return proc_1[r];
            else
                return proc_2[r];    
        }   

        @Override
        public boolean isCellEditable( int r, int c )  {
            return true;
        }        

        public void setValueAt( Object[][] value, int r, int c )  {
               value = obj;
               fireTableCellUpdated( r, c );
             }            

        }    
    }
4

1 回答 1

2

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());
        }
    }

}
于 2013-03-31T16:38:09.540 回答