File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Deleting a pane and all it's contents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Deleting a pane and all it Watch "Deleting a pane and all it New topic
Author

Deleting a pane and all it's contents

wayne morton
Greenhorn

Joined: May 17, 2012
Posts: 28
Brief explanation....I have created a GUI with a scroll pane, an add button and delete button.

When you press add it creates a new pane within the scroll pane and that pane contains various components, labels, text fields etc.

When you press delete it obviously deletes that new pane and all it's components.

Now my question is can i just use


which i am currently using and appears to work although i am concerned that this may cause issues as i have not individually removed all the components? Do they still exist and does that give potential to cause issues when i create them again?
At the moment it seems to work fine.

Or is it best to remove all components at the same time.e.g.

Or is that just being pedantic and creating unnecessary work for myself?

Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

You should have a look at the CardLayout layout manager. You could simply add a JPanel with a CardLayout to the JScrollPane, and flip it to the appropriate layout container when you press a button.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
wayne morton
Greenhorn

Joined: May 17, 2012
Posts: 28
Thanks for the reply Jelle.
From my basic description a card layout would seem like a logical piece of advice but from what i understand about card layouts it simply a system of tabs unless it is more complex that i perceive? Which is not what i want for this instance. In this case i need to be able to see multiple inner panels at the same time for easy comparison, not to be flicking between one pane and another which i would have to do with tabs.

In the case of what i have created there can be multiple inner panes with varying data for comparison and will eventually organize them.

Baring what is the best layout design my main question was to do with the best way to delete a panel and its contents which is useful for future reference at least.

In simple terms when deleting a pane can i just delete that pane itself or do i need to specifically delete all it's components also to prevent any future issue?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

You need to be sure that when you remove / add components you do so on the EDT and that you then call repaint() on the parent component (also on the EDT). I'm not sure if you need to invoke revalidate() on the parent before the call to repaint(), but I seem to recall that it's handled automagically when you add or remove components.

Edit: And to answer the actual question, no you don't have to remove every individual child component, you can simply remove the container i.e the JPanel.
wayne morton
Greenhorn

Joined: May 17, 2012
Posts: 28
Thanks again Jelle.

I already incorporated revalidate() and repaint() into the logic.
After some testing previously i found that adding both methods was the best option as depending on the setup one wasn't enough due to either not working at all or not working as intended.i.e. making a bit of a mess of my layout on updating it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Deleting a pane and all it's contents