开发者

How to empty this panel and add new panels

I have one main JPanel container and three JPanels inside. How to empty this panel and add new panels? I 开发者_如何学Gotried with remove(Component) but it doesn't work. Can anybody give me advice?


This will do it. The trick is to call revalidate.

mainPanel = ...

mainPanel.removeAll();
mainPanel.add(newPanel1);
mainPanel.add(newPanel2);
mainPanel.add(newPanel3);
mainPanel.revalidate();  

But really, consider using CardLayout, if you want to change what appears in a JPanel.


Here in this link i found a simple tutorial on how to add and remove elements from panels. The other panels inside your main panel, are also elements, so the same principle applies to them.

A good practices when adding something new in the panel is not just to use the method add(): we might also want to use revalidate() and repaint() They should be called when some event occurs(button clicked or similar...)

Also i want to mention that in the tutorial remove() i being used to remove elements, you are doing it corretly. Maybe calling again revalidate() and repaint() for the other panels make the removed panel dissapear from the GUI(The object is deleted just the GUI is not refreshed)

Note: I suppose that the elements of your inner panels are visible = true. If some of the inner elements struggle to render try to call also revalidate() and repaint() at them. I think this way should work.


@ Harry Joy

if you added or removed (already visible container) then you have to call

revalidate(); repaint(); // not required in all cases

@ Damir if JComponents isn't public (or private) static then you can just call

myContainer.removeAll();
myContainer.revalidate();
nyCOntainer.repaint();

possible is remove JComponent(s) by some parameter(s) with Component[] a = myContainer.getComponents(); then you could call if (components[i] instanceof JComboBox) { ...


Try the other remove method remove(int index);


this works 100%

this.panelname.Controls.Clear();


I too had the same problem. All I did to resolve the issue was

panelName.setVisible(false);
mainPanel.remove(panelName);

In my case, panelName is a JPanel which lies inside mainPanel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜