• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check if a component exists in Swing?

 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! How do I check if a component exists in a container and replace it if it exists. I have tried:



Please help. Thanks
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get better help sooner, post a SSCCE that demonstrates the incorrect behaviour.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Blass wrote:Hi! How do I check if a component exists in a container and replace it if it exists.


If you have a requirement to know whether a container actually contains a particular component, then you should write your application to keep track of that information. For example, when you add the component to the container, set some boolean flag to true. And when you remove it from the container, set that flag to false.
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess the problem is the GroupLayout. You can't just use:



You need to specify a bunch of constraints to control the horizontal and vertical positioning. That is why this layout is only used by IDE's that generate the code.

I would suggest you try using a differnt layout manager.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When called during runtime, remember to call revalidate() and repaint() on the JPanel. Components do not trigger any updates to be repainted when their hierarchical structure is modified.
 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your suggestions.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:When called during runtime, remember to call revalidate() and repaint() on the JPanel. Components do not trigger any updates to be repainted when their hierarchical structure is modified.

I was going to suggest that when I read the OP, but found that GroupLayout#replace takes care of it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic