添加数据作为参数后,Jtable内容不会刷新

发布于 2024-08-22 17:15:05 字数 1814 浏览 4 评论 0原文

我有一个 jtable,如果我将表数据设置为公共变量(不要将其放在 tablemodel 的参数上),一切都会正常工作。但现在我需要将表数据设置为参数,并且表不会刷新其内容。我打印出一些单元格值和行号,它们都已更新且正确。只是显示没有改变,还是老内容。我一直在尝试所有类型的方法来刷新表,包括 fireTableDataChanged 和 repaint 以及其他 fire 函数,但没有一个起作用。请帮忙。多谢!!!!!!

表模型是:

       public class MyTableModel extends AbstractTableModel {

           protected String[] columnNames;
           private String[][] data;


            public MyTableModel(String[] columnNames, String[][] data){
                this.columnNames = columnNames;
                this.data = data;
                //I add this here but it still doesn't refresh the table
                fireTableDataChanged();
            }

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

            public int getRowCount() {
                return data.length;
            }

            public String getColumnName(int col) {
                return columnNames[col];
            }

            public String getValueAt(int row, int col) {
                return data[row][col];                  
            }

           public void setData(String[][] newdata) {   
              data = newdata;

              fireTableDataChanged();   
              }  
        }

我更新表模型和表的代码是:

        tableModel=new MyTableModel(columnNames, data);
        tableModel.fireTableDataChanged();
        table = new JTable();
        table.setModel(tableModel); 
        ((AbstractTableModel) table.getModel()).fireTableDataChanged();
        //outputs showed the content is updated
        System.out.println("total row count" + table.getRowCount());
        System.out.println("cell info" + table.getValueAt(0,5));


        table.repaint();
        feedback.repaint();
        this.repaint();

I have a jtable, everything works fine if I set the table data as a public variable(don't put it on tablemodel's parameter). But right now I need to set the table data as a parameter, and the table won't refresh its contents. I print out some cell value and the rows number, they are both updated and correct. Just the display is not changed, still the old contents. I have been trying all type of methods to refresh the table, including fireTableDataChanged and repaint, and other fire functions, none of them works. Please help. Thanks a lot!!!!!!

the table model is:

       public class MyTableModel extends AbstractTableModel {

           protected String[] columnNames;
           private String[][] data;


            public MyTableModel(String[] columnNames, String[][] data){
                this.columnNames = columnNames;
                this.data = data;
                //I add this here but it still doesn't refresh the table
                fireTableDataChanged();
            }

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

            public int getRowCount() {
                return data.length;
            }

            public String getColumnName(int col) {
                return columnNames[col];
            }

            public String getValueAt(int row, int col) {
                return data[row][col];                  
            }

           public void setData(String[][] newdata) {   
              data = newdata;

              fireTableDataChanged();   
              }  
        }

The code I update the table model and table are:

        tableModel=new MyTableModel(columnNames, data);
        tableModel.fireTableDataChanged();
        table = new JTable();
        table.setModel(tableModel); 
        ((AbstractTableModel) table.getModel()).fireTableDataChanged();
        //outputs showed the content is updated
        System.out.println("total row count" + table.getRowCount());
        System.out.println("cell info" + table.getValueAt(0,5));


        table.repaint();
        feedback.repaint();
        this.repaint();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤千羽 2024-08-29 17:15:05

您不应该重新分配您的表:

 tableModel=new MyTableModel(columnNames, data);
 tableModel.fireTableDataChanged(); // not necessary
 table = new JTable(); // dont do this, you are removing your handle to the table in your view
 table.setModel(tableModel); 

You should not be reassigning your table:

 tableModel=new MyTableModel(columnNames, data);
 tableModel.fireTableDataChanged(); // not necessary
 table = new JTable(); // dont do this, you are removing your handle to the table in your view
 table.setModel(tableModel); 
堇色安年 2024-08-29 17:15:05

我将模型数据设置为静态,因为它工作正常。我不关心这部分的 OOPS..

/******************Its a custom class for creating custom editors so plz comment unwanted code ********************************/

@SuppressWarnings("serial")
class MyTableModel extends AbstractTableModel 
{
    protected String[] columnNames = {"URL",
                                    "Category",
                                    "Select"};
    protected static Object data [][] = null;

    public MyTableModel() { 
     }

    public final Object[] longValues = {"http://www.oracle.com", "BOOK", Boolean.TRUE};

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

    public int getRowCount() {

        if(data !=null)
            return data.length;
        else
            return 0;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {

        if(data!=null)
            return data[row][col];
        else
            return null;
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 0) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) 
    {

        data[row][col] = value;
        fireTableCellUpdated(row, col);
        // printDebugData();
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
}

/***********************************/
//Use a Jbutton add a listener and put this code in it to test it works !!

Object d [][] = {
                {"http://www.abc.com", "ABC", new Boolean(false)},
                {"http://www.google.com", "DEF", new Boolean(true)},
                {"http://www.yahoo.com", "FGH", new Boolean(false)},
                {"http://www.google.com", "IJK", new Boolean(true)},
                {"http://www.yahoo.com", "LMN", new Boolean(false)}
            };

        model.data = d; //Important to update data
        model.fireTableDataChanged(); //Important to make changes reflected.
/*********************************************/

I made the Model data as static as its working fine. I dont care about OOPS for this part..

/******************Its a custom class for creating custom editors so plz comment unwanted code ********************************/

@SuppressWarnings("serial")
class MyTableModel extends AbstractTableModel 
{
    protected String[] columnNames = {"URL",
                                    "Category",
                                    "Select"};
    protected static Object data [][] = null;

    public MyTableModel() { 
     }

    public final Object[] longValues = {"http://www.oracle.com", "BOOK", Boolean.TRUE};

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

    public int getRowCount() {

        if(data !=null)
            return data.length;
        else
            return 0;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {

        if(data!=null)
            return data[row][col];
        else
            return null;
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 0) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) 
    {

        data[row][col] = value;
        fireTableCellUpdated(row, col);
        // printDebugData();
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
}

/***********************************/
//Use a Jbutton add a listener and put this code in it to test it works !!

Object d [][] = {
                {"http://www.abc.com", "ABC", new Boolean(false)},
                {"http://www.google.com", "DEF", new Boolean(true)},
                {"http://www.yahoo.com", "FGH", new Boolean(false)},
                {"http://www.google.com", "IJK", new Boolean(true)},
                {"http://www.yahoo.com", "LMN", new Boolean(false)}
            };

        model.data = d; //Important to update data
        model.fireTableDataChanged(); //Important to make changes reflected.
/*********************************************/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文