Reshad
Reshad

Reputation: 2652

Recreating a JTable when updating content

I have a application that uses a JTable in it. when I add something to the database it comes into database but I am not able to recreate the JTable somehow.. I have tried to repaint(); the method that creates my table I have tried the Revalidate(); but also no success

I even tried to recall the method but also that didn't help.

The actionperformed is as follows:

    @Override
public void actionPerformed(ActionEvent e) {

    if(e.getSource() == add) {
        model.insertValue(toDo.getText());
        model.getValue();
        view.createTable();
        toDo.setText("");
    }
}

and the method that creates the JTable

    public void createTable() {

    JTable table = new JTable();

    DefaultTableModel tableModel = new DefaultTableModel(new Object[][]{},new String[]{"To do","Date added", "Modify"});

    table.setSize(450, 600);


    table.setModel(tableModel);
    JScrollPane scrlPan=new JScrollPane(table);

    for(int i = 0; i < model.getId().size(); i++) {

        tableModel.addRow(new Object[]{
                model.getItem().get(i), 
                model.getDate().get(i), 
                model.getId().get(i)
                });
    }

    add(scrlPan);
    add(table.getTableHeader(), BorderLayout.NORTH);
    add(table, BorderLayout.CENTER);
}

any ideas on how to solve this?

Upvotes: 0

Views: 1088

Answers (3)

Andremoniy
Andremoniy

Reputation: 34900

Make tableModel as a field of the class and update it when you need. Do not recreate the table object.

Upvotes: 1

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

The following code, derived directly from yours, works for me without issues:

    JTable table = new JTable();
    final DefaultTableModel model = new DefaultTableModel(
            new Object[][] {}, new String[] { "To do", "Date added",
                    "Modify" });
    table.setModel(model);

    JFrame f = new JFrame();
    f.getContentPane().add(table);

    JButton b = new JButton("More ..");
    f.getContentPane().add(b, BorderLayout.NORTH);
    b.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < 2; i++) {
                model.addRow(new Object[] { "A" + i, "B" + i, "C" + i });
            }
        }
    });
    f.setSize(400, 400);
    f.setVisible(true);
    b.doClick();

Simply check that are you doing differently. One last thing I may suspect - maybe you set the new values directly from the non Swing thread. Use SwingUtilities.invokeLater to set the values.

Models derived from AbstractTableModel need to call the inherited method fireTableCellUpdated on your model. There are also more triggers to fire content change listeners that JTable registers so it could update itself when the model changes. Use that is appropriate for your case. An alternative way that may be good for the full content change is to create and set the whole new TableModel. But in your case this may not be relevant as you use DefaultTableModel: it has this functionality implemented already, it does not need repaint(), revalidate(), fireTableXYZ. It must do everything itself as soon as you set the new content.

Upvotes: 1

mKorbel
mKorbel

Reputation: 109813

  • don't reinvent the wheel, search for ResultSetTableModel (few code workarounds) or TableFromDatabase made by @camickr

  • a.m. workaround are EDT sensitive, JTables contents waiting until ResultSet returns all rows, Swing GUI freeze or isn't responsible for Key & Mouse events until long and hard Object (JDBC) is done, this logics could be suitable for small ResultSets from small DB tables, wihtout opening & closing Connection to Database, otherwise have to use SwingWorker and Batch logics (f.e. update XxxTableModel with 20rows)

Upvotes: 1

Related Questions