Jtable not updating with my abstracttablemodel

扶醉桌前 提交于 2019-11-28 06:47:08

问题


I am new at programming and am working on my first school assignment. I have written a gui that accepts input and outputs data in a jtable added to a jpaddedpane. When the table first appears it shows all the correct data. But when I enter new input the table won´t update. I am alsmot positive the problem lies with my implementation of AstractTableModel. Can someone please take a look and correct it for me asap? Thanks in advance.

ps. nh, vh, hNam, proc_1 and proc_ are integer, string, integer, string and string arrays respectively. They hold the data to be displayed in the table.

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

        }    
    }

回答1:


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

}


来源:https://stackoverflow.com/questions/15731045/jtable-not-updating-with-my-abstracttablemodel

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