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?
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.
Joined: May 17, 2012
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?
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.
Joined: May 17, 2012
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.