Question

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.

Was it helpful?

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.

OTHER TIPS

The solution to your problem (and the previous one) is rather simple, but unfortunately a bit too long to post it in a comment

  1. You make sure you do some reading on how a JTable actually works. A good starting point is the Swing tutorial and perhaps even the class javadoc of the most important classes JTable and TableModel. Another good read about Swing and the use of MVC is this document. It is much broader then only JTables but will provide you with more insight in how all Swing components are designed, and how you should use them.
  2. After reading that tutorial, you should understand that if you want to update the data which is shown in the table, you update the model of your table. The model will fire events which are received by the table, and the table will make sure it updates itself. After all, the table is only a view on the data (=the TableModel) so updates on the take should take place on the model side. If this is not clear, go back to step 1
  3. Now take a look at your code and remove the listener. Instead, update the model on the table and see how the table updates itself.
  4. If this does not work, you come back to this site with an SSCCE which you post. That way we can see that you tried something, what you tried and most likely what is wrong in your code. This SSCCE should include your code of the TableModel you use and the code that updates your TableModel since that is the most likely location for your problem.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top