• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Deleting a pane and all it's contents

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic