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

只谈情不闲聊 提交于 2019-12-13 01:03:28

问题


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.

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.

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.


回答1:


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



来源:https://stackoverflow.com/questions/12955605/repaint-revalidate-invalidate-how-to-use-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!