tags:

views:

661

answers:

2

Hi, 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();
+3  A: 

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); 
akf
Yes!!! Thanks!!!!
blue igloo
A: 
Kalpit