JonasP
JonasP

Reputation: 51

JTable refresh are not displayed

I'm desperately trying to convince my JTable to refresh when I change its data. The data are stored in a global singleton, TreeSet, that I'm working with. Whenever the TreeSets data is altered, an event is fired to refresh the TableModel. For test purposes, I replaced this by a simple Timer firing events.

Each time an event is fired, an element from the TreeSet is removed to simulate changes in the data. The event fires and the TableClass receives it and processes it as expected, but when it comes to refreshing nothing happens. I tried to create a new TableModel each time an event occurs and set it to the global table. The changes to the singleton TreeSet are made but nothing happens to the JTable.

public class TreeTableObj implements ActionListener{
public JTable table ;

public TreeTableObj(){
    MyTableModel myModel = new MyTableModel(getValuesOfTreeSet(), getTreeSetRows());

    table = new JTable( myModel){ //some rendering };

    table.setAutoCreateRowSorter(true);
    }

    class MyTableModel extends AbstractTableModel {
        private String[] columnNames;
        private Object[][] data ;


        public MyTableModel(Object[][] data, String[] columnNames){
            this.data = data;
            this.columnNames = columnNames;
            System.out.println("Model created");
        }

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

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

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

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

        public void setData(Object[][] data){
            this.data = data;
            fireTableDataChanged();
        }


        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }


        public boolean isCellEditable(int row, int col) {
            if (col < 2) {
                return false;
            } else {
                return true;
            }
        }


        public void setValueAt(Object value, int row, int col) {
            if (true) {
                System.out.println("Setting value at " + row + "," + col
                                   + " to " + value
                                   + " (an instance of "
                                   + value.getClass() + ")");
            }

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

            if (true) {
                System.out.println("New value of data:");
                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("--------------------------");
        }
    }

    public void refreshTableModel(){
        FlightsTreeSet.getInstance().remove(FlightsTreeSet.getInstance().first());
        table.setModel(new MyTableModel(getValuesOfTreeSet(), getTreeSetRows()));
        }

    public void actionPerformed(ActionEvent arg0) {
        refreshTableModel();    
    }
}

Any help would be appreciated!

[EDIT1]

I am getting closer to the problems Core: I found out that the table which is displayed in the JFrame holds another reference as the one that I change. I made all changes visible and they apply to the Model.

Upvotes: 3

Views: 713

Answers (2)

JonasP
JonasP

Reputation: 51

I found the problem. And leaving the answer here to be found by others... After realizing that the displayed JTable and the one that I was editing hold different references (see [EDIT1]) I found out that I was creating two different Objects of the JTable. One to be displayed and the other one in main() to become an ActionListener. The Listener received the Events and did the changes but the other one never noticed that anything was happening.

Upvotes: 1

Russell Zahniser
Russell Zahniser

Reputation: 16364

Is something missing in your code? Your table model as it is shouldn't compile, because it is missing all the methods that JTable would use to access the data (getColumnCount(), getValueAt(), etc.)

Also, you shouldn't have to create a new model on every change - just have the model fireTableStructureChanged().

Upvotes: 2

Related Questions