File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes How can I destroy a JPanel component? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How can I destroy a JPanel component?" Watch "How can I destroy a JPanel component?" New topic

How can I destroy a JPanel component?

Jess Ishasder

Joined: Feb 19, 2002
Posts: 21
Suppose I have a panel and 2 buttons on it. If buttonA is pressed, a textfield component will be displayed. Similarily, when another button, buttonB is pressed, a textfield and a list components will be displayed.
I would like to know how can I destroy those previous appeared components and let other components display on the panel?
Can I display different panels depends on which button is pressed? But the problem is how can I destroy the previous appeared panel?
Thank you for your help!!!

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
JPanel inherits:
validate() from class Container which ensures that this component has a valid layout.
setVisible(boolean b) from class JComponent which shows or hides this component depending on the value of parameter b. Components in other parts of the AWT/Swing class heirarchy also have a setVisible(boolean) method available.
So, if you were to set the visibility to off, setVisible(false), then invoke validate() on your JPanel (Container), that will hopefully do the trick.
Good Luck.
[ March 13, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

Or you could look into CardLayout... it's kinda hard to understand at first, but it encapsulates all the showing, hiding, and ordering of components... it's probably the easiest way to provide you with a way to flip between Panels on a GUI...


Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Colin Humber

Joined: Nov 05, 2001
Posts: 6
JPanel is a container. Container has a method called remove(Component c). This method removes the component from the container.

Sun Certified Programmer for the Java 2 Platform.<br />Sun Certified Programmer for the Java 2 Platform, 1.4.<br />Sun Certified Web Component Developer<br />IBM Certified Specialist VisualAge for Java.<br />IBM Certified Solution Developer (OOAD w/ UML).
I agree. Here's the link:
subject: How can I destroy a JPanel component?
It's not a secret anymore!