aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Removing a JComponent Dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Removing a JComponent Dynamically" Watch "Removing a JComponent Dynamically" New topic
Author

Removing a JComponent Dynamically

john latham
Greenhorn

Joined: May 28, 2006
Posts: 22
Now that i can add a component dynamically, i would like to also remove it when needed.

How would i go about this, currently this is what im doing in my actionPerformed method:

if the checkbox is checked add the graph to the panel, if unchecked remove it from the panel. The problem is im making the panel on the fly so i cant use .remove to get rid of it, or can I? Here is my attempt


[ September 07, 2006: Message edited by: john latham ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
a lot depends on where this is
LineGraphGui l = new LineGraphGui("Node "+row, "Time", "Temperature");

if it is part of the method that adds, then remove(l) will not be referring to
the previously added (l), but to the new (l) just created.

here's a simple demo using a checkbox to add/remove

john latham
Greenhorn

Joined: May 28, 2006
Posts: 22
Hmm i see what you mean, but is it possible to add something i create as an anyonous class and remove it? Because i dont want to make a graph unless the user asks for it.

like this:

if(graph) {
lineGraphPanel.add(new LineGraphGui("Node "+row, "Time", "Temperature"));
lineGraphPanel.validate();
}

else {
this.lineGraphPanel.remove(???);
lineGraphPanel.validate();
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
try this
(but instanceof can have it's own little problems, if not careful)

john latham
Greenhorn

Joined: May 28, 2006
Posts: 22
It works, thank you very much!! Ive never used instanceof before, its very useful.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing a JComponent Dynamically