aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to check if a component exists in Swing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to check if a component exists in Swing?" Watch "How to check if a component exists in Swing?" New topic
Author

How to check if a component exists in Swing?

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
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: 4523
    
    5

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: 18155
    
    8

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: 2095
    
    7
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: 19556
    
  16

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Thanks all for your suggestions.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to check if a component exists in Swing?
 
Similar Threads
SOME1 HELP ME URGENT- NullPointerException
Pesky Group Layout
Builing First GUI - A few questions
BorderLayout problem with NullLayout
BorderLayout problem with NullLayout