Search code examples
javaswingjtablejframedefaulttablemodel

JTable won't refresh


Why won't table cell 0,1 change from aaa to XXXX?

import java.awt.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;


class MainFrame {

    public static void main(String[] args) {


        JFrame f = new JFrame("Refreshing JTable");
        JPanel p = new JPanel(new GridBagLayout());
        DefaultTableModel productsModel;
        JTable productsTable;

        f.setSize(800, 600);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        String[] tableTitle = new String[] {"ID", "Name"};
        String[][] tableData = {{"1", "AAA"},{"2", "BBB"}};


        productsModel = new DefaultTableModel(tableData, tableTitle);
        productsTable = new JTable(productsModel) {
            public boolean isCellEditable(int r, int c) {
                return false;
            }
        };

        JScrollPane scrollpane = new JScrollPane(productsTable);


        tableData[0][1] = "XXXX";

        f.add(p);
        p.add(scrollpane);
        f.validate();
        f.setVisible(true);


    }


}

REASON: Apparently trying to update the array where data is stored will result in JTable not changing. Either DefaultTableModel needs to be updated or the whole table needs to be redrawn.

EDIT (possible Solution) One way is using Timer:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

class MainFrame {

    static JFrame f = new JFrame("Refreshing JTable");
    static JPanel p = new JPanel(new GridBagLayout());
    static DefaultTableModel productsModel;
    static JTable productsTable;

    public static void main(String[] args) {
        runGui();
    }

    @SuppressWarnings("serial")
    public static void runGui() {

        f.setSize(800, 600);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        String[] tableTitle = new String[] {"ID", "Name"};
        String[][] tableData = {{"1", "AAA"},{"2", "BBB"}};


        productsModel = new DefaultTableModel(tableData, tableTitle);
        productsTable = new JTable(productsModel) {
            public boolean isCellEditable(int r, int c) {
                return false;
            }
        };

        JScrollPane scrollpane = new JScrollPane(productsTable);


        tableData[0][1] = "XXXX";

        Timer t = new Timer(2000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addColumns();
                remakeData();
                productsTable.setModel(productsModel);

            }
        });
        t.start();

        f.add(p);
        p.add(scrollpane);
        f.validate();
        f.setVisible(true);


    }

    private static void addColumns() {
        productsModel.setColumnCount(0);
        productsModel.addColumn("ID");
        productsModel.addColumn("Name");
    }

    private static void remakeData() {
        productsModel.setRowCount(0);
        productsModel.insertRow(productsModel.getRowCount(), new Object[] {"1", "Dummy item 1"});
        productsModel.insertRow(productsModel.getRowCount(), new Object[] {"2", "Dummy itme 2"});
        productsModel.insertRow(productsModel.getRowCount(), new Object[] {"3", "Dummy item 3"});
        productsModel.insertRow(productsModel.getRowCount(), new Object[] {"4", "Dummy item 4"});
        productsModel.insertRow(productsModel.getRowCount(), new Object[] {"5", "Dummy item 5"});
    }
}

EDIT(much better solution, the way it worked for me flawlessly) Using a static method. Since I'm adding new data in array through another Frame, I created a static method in MainFrame, which I call every time I add/update/delete Object in array. This method will redo the whole model after update and will therefore refresh table.


Solution

  • One problem with the SSCCE posted on your related thread is that the code changes the array which originally formed the table model, whereas it should be changing the table model itself.