1

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.

Community
  • 1
  • 1
Amarnath
  • 8,736
  • 10
  • 54
  • 81

1 Answers1

4
  • 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....

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Sorry Sorry. I am doing the same thing as u said. I posted it wrongly. I am only getting a updated model. – Amarnath Oct 18 '12 at 13:58
  • @Che call revalidate() and then repain() as suggested by mKorbel. If this does not work for you, edit your post with an [SSCCE](http://sscce.org) that reproduces your issue. – Guillaume Polet Oct 18 '12 at 14:47
  • then there are valid my point second, no idea what are coded in your PC – mKorbel Oct 18 '12 at 14:48
  • 1
    This related [example](http://stackoverflow.com/a/8260663/230513) leaves the table in place and just changes the model using `setModel()`. Because the table listens to its own model, no additional methods are required. – trashgod Oct 18 '12 at 17:56
  • The problem is with the dialog. I am using the old reference of the dialog. Anyways thanks for the suggesstions. I am accepting answer for the time you have taken. – Amarnath Oct 19 '12 at 06:54
  • @Che wrong, wrong, never to create a new JDialog on the Fly, reuse that for anther action, bacause [these Object stays in the JVM memory](http://stackoverflow.com/questions/6309407/remove-top-level-container-on-runtime) – mKorbel Oct 19 '12 at 07:03
  • I changed my appraoch (little funny one, but looking good.) Now I am not creating new dialogs. Only one dialog instance and I am able to refresh the data in the table. Thanks for the "Object stays in the JVM memory" link. Its a very good post .. :) – Amarnath Oct 19 '12 at 09:15
  • 1
    @Che then only next one milion good decisions and you'll be good programmer :-), Friday == FunnyDay – mKorbel Oct 19 '12 at 09:59
  • Thank u .. have a great weekend .. :-) – Amarnath Oct 19 '12 at 10:11