Jtable没有使用我的abstracttablemodel进行更新 [英] Jtable not updating with my abstracttablemodel

查看:120
本文介绍了Jtable没有使用我的abstracttablemodel进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,正在完成我的第一份学校作业。我写了一个gui,接受输入并输出添加到jpaddedpane的jtable中的数据。当表格首次出现时,它会显示所有正确的数据。但是当我输入新输入时,表格不会更新。我是alsmot积极的问题在于我的AstractTableModel的实现。有人可以看看并尽快为我纠正吗?在此先感谢。

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和proc_分别是整数,字符串,整数,字符串和字符串数组。它们保存要在表中显示的数据。

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

        }    
    }


推荐答案

这就是问题所在的地方 value = obj;

This is where the issue is value = obj;

In setValueAt 方法您没有将值设置为相应的 obj 值。您访问 getValueAt 的方式类似地将获得的值设置为相应的数组位置。

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.

使用 ArrayList 而不是使用数组。您可以轻松访问所有方法。

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.
}

这是使用ArrayList的TableModel的一个例子。

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

}

这篇关于Jtable没有使用我的abstracttablemodel进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆