0

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Reshad
  • 2,570
  • 8
  • 45
  • 86

3 Answers3

1

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

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • i've tried this.. but didn't work. I think that I have implemented it the wrong way. could you give me an example ? – Reshad Jan 09 '13 at 13:52
  • @Reshad try `tableModel.fireTableDataChanged()` after changing model. Also look at this: http://stackoverflow.com/questions/3179136/jtable-how-to-refresh-table-model-after-insert-delete-or-update-the-data – Andremoniy Jan 09 '13 at 14:17
  • 1
    no, never ever call any of the fireXX methods from code external to the model - it's the _model_ responsibility to notify its listeners on change – kleopatra Jan 09 '13 at 15:33
  • @kleopatra, Why did you said *"never ever call any of the fireXX methods from code external to the model"*? How can you explain this? Furthermore, I have provided link to the question and its top-rating answer. Will you argue with that experts? – Andremoniy Jan 09 '13 at 17:07
  • OP is using _DefaultTableModel_, so there is no need to call _fireXX_ method explicitly. _DefaultTableModel_ itself does that when ever there is a change in the table data. AFAIK what ever you do on the table's data, it should be done on the _tableModel_. So your table model should call the _fireXX_ methods when ever it is updated. I think that is the reason why _kleopatra_ said that. – Amarnath Jan 09 '13 at 17:21
1

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.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • On what part of my Model should I need to use the fireTableCellUpdated function? because my Model doesn't know anything about the JTable all of that is in my view. the Model only knows database handling and pushing to the view. – Reshad Jan 09 '13 at 13:42
  • Hmm ... if you mean to call fireXX from code _outside_ of the tableModel - that's a no-no: it's up to the model itself to notify its listeners. Doing so _inside_ of a custom tableModel implementation is the way to go :-) – kleopatra Jan 09 '13 at 13:51
  • Your model knows enough about its table as it is derived from AbstractTableModel and has inherited code. Most important, it inherits addTableModelListener that is called by JTable when you set a model for it. Just call on your model: myModel.fireTableCellUpdated(); . – Audrius Meškauskas Jan 09 '13 at 13:52
  • @AudriusMeškauskas ah wait I misunderstood.. my bad! but what should I do with the parameters that I have to give into the myModel.fireTableCellUpdate();? cause it can not be empty :S what eclipse suggests is to use fireTableChanged(); that requires no parameters but when using that nothing happens actually.. – Reshad Jan 09 '13 at 14:02
  • If you are not sure where and what has changed, or maybe everything has changed, it may be simpler to recreate and set the whole new model. – Audrius Meškauskas Jan 09 '13 at 14:05
  • here's what I did I made this protected DefaultTableModel tableModel; and I call it as view.tableModel.fireTableStructureChanged(); in my actionPerformed but still no changes. – Reshad Jan 09 '13 at 14:07
  • 1
    @Reshad don't ever to call fireTableXxx for DefaultTableModel, this notifier is implemented and correctly – mKorbel Jan 09 '13 at 14:19
  • hmm so basically I don;t have to do anything? ( that doesn't work either btw ) – Reshad Jan 10 '13 at 22:54
1
  • 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)

mKorbel
  • 109,525
  • 20
  • 134
  • 319