Search code examples
javaswingjtablepanelrepaint

repaint(), revalidate(), invalidate(). How to use them?


I have problem with display of table. My table initially has rows. But next time if I do some operation my table list becomes empty. So when I add that model to the table the table is empty. It is empty but the display is showing white colour in table area.

Initially the table looks like below figure.

First Table

Then I need to call some action and if that action returns an empty list then I am creating a new Jtable with that list and placing it on the panel. But it is looking like the following.

Empty Table

Following is the approach which I am follwoing to do this,

pnlRight.removeAll();
//pnlRight.invalidate();
// pnlRight.revalidate();
// pnlRight.repaint();
pnlRight.add(new JScrollPane(table), BorderLayout.CENTER);
pnlRight.invalidate();
pnlRight.validate();
pnlRight.repaint();

I have read one of the post on this @kdgregory, after removing I should call the above methods. this weird thing is happening. If I call only repaint() then even the column names are missing from the panel (Ideally this should not happen in this case.) Please explain what is the problem with this.


Solution

    • not I think there isn't (logical) reason for that, to remove a JTable (with JScrollPane) and put there another JTable

    • create JTable and to store all data to XxxTableModel,

    • reset XxxTableModel and then to load another data in different structure

    • use CardLayout for switching betweens views

    EDIT

    • following code

    pnlRight.revalidate();

    pnlRight.repaint();

    there no caused any issue, problem isn't in this code....