This discussion is archived
10 Replies Latest reply: Sep 5, 2012 6:55 AM by jduprez RSS

Refresh data in a jtable when data changes

888525 Newbie
Currently Being Moderated
I have a jtable grdTable and a tableModel mytableModel which extend abstract table model

I have a jcheckbox in the column 1 which i want to uncheck all
I am doing the following.The data is changed ,I could see by doing susyem.out.println() but the data in the table does not refreshes.

public void showTablechanges()
{
TableModel tm = grdTable.getModel;
for(int i=0 ;i<grdTable.getRowCount();i++)
{
grdTable.setValueAt(Boolean.FALSE, i, 1);
}
( (MyTableModel)tm).fireTableDataChanged();

grdTable.repaint();
}


What am i missing or what wrong I am doing.How can i make the data refresh.

I tried do a sys out after seting the value and it is showing as false but the checkbox is still checked.I have tried a lot of things but I am not able to refresh the jtable.

Please please help.

Thanks
  • 1. Re: Refresh data in a jtable when data changes
    EJP Guru
    Currently Being Moderated
    You don't need to fire the event or issue the paint. It should just work. Provided you are doing this on the event thread.
  • 2. Re: Refresh data in a jtable when data changes
    888525 Newbie
    Currently Being Moderated
    Thanks so much for your reply.
    I tried without the repaint as well but it doesnt seem to work.
    Looks like I am missing to do something
    I am new can you explain what do you mean by event thread and how to do it via event thread.


    Thanks,
  • 3. Re: Refresh data in a jtable when data changes
    gimbal2 Guru
    Currently Being Moderated
    All you need to know about Swing:

    http://docs.oracle.com/javase/tutorial/uiswing/

    and specifically to answer your question:

    http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html
  • 4. Re: Refresh data in a jtable when data changes
    888525 Newbie
    Currently Being Moderated
    Thanks so much for the reply I read the links.I modified my code as

    public void showTablechanges() {
    SwingUtilities.invokeLater(new Runnable() {

    public void run() {
    TableModel tm = grdTable.getModel;
    for (int i = 0; i < grdTable.getRowCount(); i++) {
    grdTable.setValueAt(Boolean.FALSE, i, 1);
    }
    ((MyTableModel) tm).fireTableDataChanged();

    grdTable.repaint();
    }
    });
    }



    This makes no difference.What strange thing is happening out of 12 checkboxes ,11 are unchecked but the first one always remains checked.EVen without the
    invoke later 11 are unchecked and 1 remains checked

    This method is in a panel and this panel is invoked from another MAin panel which has a changelistener for this panel.
    i.e if the method above is in myPanel.java then in my main panel,I have
    myPanel.addChangeListener(this)
    Also if i remove the line


    ((MyTableModel) tm).fireTableDataChanged();

    All checkboxes remains checked.
    I am really confused and this looks very tough to me ,Can you find out what is wrong

    Thanks,
  • 5. Re: Refresh data in a jtable when data changes
    jduprez Pro
    Currently Being Moderated
    It may be a bug in your custom AbstractTableModel subclass, or a bug in the custom renderer (if any), or something else...
    We don't see how you cerate the JTable. Can you post an SSCCE (http://sscce.org)?
  • 6. Re: Refresh data in a jtable when data changes
    EJP Guru
    Currently Being Moderated
    ((MyTableModel) tm).fireTableDataChanged();
    You don't need to fire the event.
    grdTable.repaint();
    You don't need to issue the paint.

    I seem to be repeating myself for some reason.
  • 7. Re: Refresh data in a jtable when data changes
    DrClap Expert
    Currently Being Moderated
    EJP wrote:
    ((MyTableModel) tm).fireTableDataChanged();
    You don't need to fire the event.
    Actually, I believe you do need to fire appropriate events if you subclass AbstractTableModel. However DefaultTableModel will take care of firing events itself.
  • 8. Re: Refresh data in a jtable when data changes
    888525 Newbie
    Currently Being Moderated
    Thanks so much for the reply.
    If I do not do fire events then the changes dont show up.I tried that also.
    If i dont fire events then what will make the changes to the table?
  • 9. Re: Refresh data in a jtable when data changes
    800268 Expert
    Currently Being Moderated
    DrClap wrote:
    Actually, I believe you do need to fire appropriate events if you subclass AbstractTableModel.
    Yes, but the firing should be done in MyTableModel#setValueAt(), not by methods which call setValueAt() like the OP has.
  • 10. Re: Refresh data in a jtable when data changes
    jduprez Pro
    Currently Being Moderated
    Yes but it's more a good design practice, doing otherwise is not a bug in itself.
    And as described by the OP, even with the firing of the event, awkward as it is, the table doesn't display the relevant updates.

    OP (885522), I suggested that you might have a bug in your custom AbstractTableModel subclass.

    1) You do realize that you have to override setValueAt(...) , since AbstractTableModel's implementation doesn't do anything?
    You probably have done so, since you see some update (just not the ones you expect)

    2) Have you unit-tested you custom model class, and checked that when you call setValueAT(x,y,something), subsequent calls to getValueAt(x,y) return the expected data? And getValueAt(x-1,y) etc... return their own individual data, not the same as getValueAt(x,y) ?

    3) If you still don't find the bug, I suggest you heed my SSCCE advice.

    Best regards,

    Jérôme

    Edited by: jduprez on Sep 5, 2012 3:53 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points