| Author |
How to check if a component exists in Swing?
|
Alan Blass
Ranch Hand
Joined: Mar 21, 2010
Posts: 106
|
|
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
Joined: May 03, 2008
Posts: 4167
|
|
To get better help sooner, post a SSCCE that demonstrates the incorrect behaviour.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
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
Joined: Jun 13, 2009
Posts: 1791
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Alan Blass
Ranch Hand
Joined: Mar 21, 2010
Posts: 106
|
|
|
Thanks all for your suggestions.
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4167
|
|
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.
|
 |
 |
|
|
subject: How to check if a component exists in Swing?
|
|
|