Rory Lester
Rory Lester

Reputation: 2918

Problems updating jtable java

i made a class where i create my jtable. I have some issues because when i try to update it, it does not work (nothing happens - i think the way i am trying to update it is wrong?). What am i doing wrong?

class tableClass
{
    public Vector rowData = null; 
    public Vector columnNames = null; 
    private JTable jTable; 
    DefaultTableModel model;

    public tableClass(JPanel jPanel, Vector rowDataInput, Vector columnNamesInput)
    {       
        rowData = rowDataInput; 
        columnNames = columnNamesInput;

        jTable = new JTable(rowData, columnNames);
        jTable.setFillsViewportHeight(true);

        JScrollPane jScrollPane = new JScrollPane(jTable);
        jScrollPane.setPreferredSize(new Dimension(300, 100));
        jPanel.add(jScrollPane,BorderLayout.CENTER);
    }

    public void updateTable(Vector rowDataInput)
    {
        rowData = rowDataInput;  
        model =(DefaultTableModel)jTable.getModel();  
        model.fireTableDataChanged();
    }
}

Upvotes: 1

Views: 3047

Answers (3)

alpian
alpian

Reputation: 4748

rowData is given to the JTable in the constructor. Then in updateTable you simply update the rowData field in your TableClass. Therefore neither the JTable nor its TableModel knows anything about your changes. You need to update the table's model in updateTable.

In other words, when you update rowData in updateTable you are not updating the same rowData object you passed into the JTable in your constructor.

The below class is functionally the same as your class. Does this make it clearer why what you're doing has no effect? You see, rowData is in no way shared by TableClass and the JTable. And it shouldn't be - you should be updating the TableModel. You can implement your own TableModel and update that object with your changes.

class tableClass
{
    public Vector columnNames = null; 
    private JTable jTable; 
    DefaultTableModel model;

    public tableClass(JPanel jPanel, Vector rowDataInput, Vector columnNamesInput)
    {       
        Vector rowData = rowDataInput; 
        columnNames = columnNamesInput;

        jTable = new JTable(rowData, columnNames);
        jTable.setFillsViewportHeight(true);

        JScrollPane jScrollPane = new JScrollPane(jTable);
        jScrollPane.setPreferredSize(new Dimension(300, 100));
        jPanel.add(jScrollPane,BorderLayout.CENTER);
    }

    public void updateTable(Vector rowDataInput)
    {
        Vector rowData = rowDataInput;  
        model =(DefaultTableModel)jTable.getModel();  
        model.fireTableDataChanged();
    }
}

You could change your code as follows to prove that the changes work. Note that i'm not advocating this approach - you should really implement your own TableModel, but simply showing you how to change the underlying model itself and therefore see the changes take place in the JTable on-screen.

EDIT As this answer was accepted, and following comments from @kleopatra and using @Adam's answer (+1) i've update the below code to show how to update the entire data Vector or Vectors in updateTable. I still would not do it like this in production code, but at least this is functionally correct and closer to the OPs question.

class MyTable {
    private Vector columnNames = null; 
    private JTable jTable;

    public tableClass(JPanel jPanel, Vector rowDataInput, Vector columnNamesInput) {
        // ...
        jTable = new JTable(rowData, columnNames);
        // ...
    }

    public void updateTable(Vector rowDataInput) {
        model =(DefaultTableModel)jTable.getModel();

        // Now actually update the model with your new data:
        model.setDataVector(rowDataInput, columnNames);
    }
}

As i said, this isn't a good way to do it, but i'm just trying to show you why what you're doing is not working. :)

Upvotes: 1

Adam
Adam

Reputation: 36703

To point you in the right direction here is a slightly cleaner working version of your class. If you've got a lot of data please be aware updating tables like this is not efficient! You'd be better subclassing AbstractTableModel yourself and mapping to whatever data structures you have already.

public class Table {
    public Vector<String> columnNames;
    private JTable table;

    public Table(JPanel panel, Vector<Vector<Object>> rowData,
            Vector<String> columnNames) {
        this.columnNames = columnNames;

        table = new JTable(rowData, columnNames);
        table.setFillsViewportHeight(true);

        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(300, 100));
        panel.add(scrollPane, BorderLayout.CENTER);
    }

    public void updateTable(Vector<Vector<Object>> rowData) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.setDataVector(rowData, columnNames);
    }

Upvotes: 2

assylias
assylias

Reputation: 328598

When you write:

rowData = rowDataInput;

it does not change the rowData you passed to the jTable in the constructor.

Upvotes: 0

Related Questions