0

I have a JTable and I can't manage to make it update its content, I am in IntelliJ IDEA.

I'll share some code first

I have all the objects declared in the main class, which extends a JFrame

private Object[][] transactionsData;
private String[] transactionsColumnNames = new String[] {"uid","data","ora","product","ini","amount","final"};
private DefaultTableModel transactionsModel;
private JTable transactionsTable;

Then in the createUIComponents() method, that I managed to understand, is called at the very beginning, when the UI needs to be created, I initialize the table like this:

    transactionsData = Main.db.getTransactions();
    transactionsModel = new DefaultTableModel(transactionsData,transactionsColumnNames);
    transactionsTable = new JTable(transactionsData,transactionsColumnNames){
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            Component component = super.prepareRenderer(renderer, row, column);
            int rendererWidth = component.getPreferredSize().width;
            TableColumn tableColumn = getColumnModel().getColumn(column);
            tableColumn.setPreferredWidth(Math.max(rendererWidth + getIntercellSpacing().width, tableColumn.getPreferredWidth()));
            return component;
        }

    };

    transactionsTable.setRowSelectionAllowed(true);
    transactionsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    transactionsTable.setDefaultEditor(Object.class, transactionsEditor);
    transactionsTable.getTableHeader().setFont(new Font("Segoe",Font.PLAIN,16));

    sorter = new TableRowSorter<>(transactionsTable.getModel());
    transactionsTable.setRowSorter(sorter);
    transactionsTable.getSelectionModel().addListSelectionListener(e -> {
        if ( transactionsTable.getSelectedRows().length > 0 ) {
            logUIDField.setText(transactionsTable.getValueAt(transactionsTable.getSelectedRow(), 0).toString());
            transactionsTable.clearSelection();
            uidFilter();
        }
    });

Now, each time I click a button i switch the content of the frame and the table is displayed, I need it to update its content before being displayed, because its content is taken from an online database, and of course, its content may have changed, so I do this:

    storicoButton.addActionListener(e -> {

        if( Main.db.hasTag() )
            logUIDField.setText(Main.db.tag.getUid());
        else
            logUIDField.setText("");

        transactionsData = Main.db.getTransactions();
        transactionsModel = new DefaultTableModel(transactionsData, transactionsColumnNames);
        transactionsTable.setModel(transactionsModel);

        uidFilter();

        CardLayout cardLayout = (CardLayout) rootPanel.getLayout();
        cardLayout.show(rootPanel, "Card4");
    });

But I can't manage to make it update its content, I tried everything I could find on the internet, and on this site, but everyone says to just use setModel, but it doesn't work, and I can't find what I'm doing wrong.

I tried to read the sizes of transactionsData, transactionsModel and transactionsTable before and after I click the button, transactionsData and transactionsModel actually update, since their size changes accordingly ( I checked looking at the database while I ran the program ) but transactionsTable doesn't change at all.

I tried repaint(), various methods that looks like "updateSomething", I tried with local variables, global variables, initializing new model, updating the model, initializing the table with the model or with data and columns, nothing. I'm desperate.

I hope someone here can help me.

At the moment here is late, and tomorrow I need to wake up early, after work I'll try starting clean, rewriting everything from scratch, maybe I'll find the solution myself, in the mean time I hope someone here could push me in the right direction at least.

Il post the code of that uidFilter() which I found online, it's the first thing I'm going to remove tomorrow, because I suspect it can change something, but I don't have the time to do that now

private void uidFilter() {
    RowFilter<TableModel, Object> rf = null;
    //If current expression doesn't parse, don't update.
    try {
        rf = RowFilter.regexFilter(logUIDField.getText(), 0);
    } catch (java.util.regex.PatternSyntaxException e) {
        return;
    }
    sorter.setRowFilter(rf);
}
  • 1
    Updating the model is all that is required as long as you're either creating a new model, or updating a DefaultTableModel or an AbstractTableModel that has been wired correctly. If this isn't working, are you sure that you're changing the model of the correct visualized JTable instance? For better help, extract out a valid [mcve] and post it here, code we can compile, run, test and modify. – Hovercraft Full Of Eels Aug 06 '17 at 23:50
  • 1
    Since you're calling `setModel(...)` on your transactionsTable, then this **will** update the JTable, but again only if you're calling it on the correct reference. My bet is that you're not, but impossible to tell based on code snippets. Again, do consider creating and posting that [MCVE](https://stackoverflow.com/help/mcve) – Hovercraft Full Of Eels Aug 06 '17 at 23:52
  • @HovercraftFullOfEels I'm pretty sure, as I said, tomorrow after work (I'm working on this project, out of my work time, it's not my main job) I'll try rebuilding the table from scratch, I'll post a minimal example here, if I still can't find a solution myself. Thanks for your time :) – Antonio Mancini Aug 06 '17 at 23:53
  • See ftp://ftp.intellij.net/pub/.idea/JTableSample.zip. – CrazyCoder Aug 07 '17 at 00:07
  • 1
    Here's a complete working [example](https://stackoverflow.com/a/8260663/230513) for reference. If your question is not a duplicate, please edit your question to include a [mcve] that shows your revised approach. – trashgod Aug 07 '17 at 00:55
  • I had very little time to work on it today, but i tried initializing a new table, copy my table in it, edit the new table, add this new table in the JScrollPane instead of the original table, but it doesn't work. I suspect it has something to do with the IDE and its GUI form... Again, when i'll have some time I'll post a MCVE, hoping that doing that, i will come up with a solution myself, it often goes like that – Antonio Mancini Aug 07 '17 at 15:36
  • `"I suspect it has something to do with the IDE and its GUI form"` -- it has nothing to do with this and again all to do with your working with the wrong references. We can't help you with specifics yet though with your question as written. – Hovercraft Full Of Eels Aug 08 '17 at 03:38

0 Answers0