Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

java - Jtable not updating with my abstracttablemodel

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

        }    
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...