aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Removing component Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Removing component " Watch "Removing component " New topic
Author

Removing component

blingo james
Greenhorn

Joined: Oct 12, 2008
Posts: 25
Hello,
Working with AWT,
Have a panel containing some other panels,
When I create it I use a .add(sub-panel) and "forget" the sub-panel.

On later stage I would like to replace the sup-panel with some other
sub-panel.
The problem is that I don't know what to write in the Remove of the main panel...

I've heard about re-creating the entire main panel, but I have data on it's
other panels, and also it seems like an over-kill...

What's the best way to deal with it?

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Best practice is to keep a reference to the current panel at all times.

If you can't for whatever reason, you can use getComponents(), getComponent(int index) and getComponentCount() to retrieve one or all components of a Container. Use those to find your sub panel.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
blingo james
Greenhorn

Joined: Oct 12, 2008
Posts: 25
Awesome thanks.
blingo james
Greenhorn

Joined: Oct 12, 2008
Posts: 25
o.k
so I have the reference to the subpanel,
I want to change the subpanel so I:


now here's the thing,
I get to see the new subpanel only when I resize the window with my mouse...

I've tried:


none worked...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

I think revalidate() needs to be called as well.
blingo james
Greenhorn

Joined: Oct 12, 2008
Posts: 25
Thanks,
Strangely I didn't find revalidate method in my panel(?!)
but I've looked validate up so finally I've found that:


does the job.

Edit: I thought that validate also causes the container to reprint
, but I saw online that both revalidate() and repaint() should be called,
but I don't find revalidate(), so I don't know what to do.
Yes it is working now, but maybe I should "do it right"?
[ October 13, 2008: Message edited by: blingo james ]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by blingo james:
Thanks,
Strangely I didn't find revalidate method in my panel(?!)


Not so strange as you are working with AWT and revalidate is a method from the Swing class, JComponent. And this leads me to my question: why AWT and not the more robust Swing? Also, have you considered using CardLayout to swap panels?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Originally posted by pete stein:
Not so strange as you are working with AWT and revalidate is a method from the Swing class, JComponent.

I usually (read: always) use Swing instead of AWT for my GUI programming, so that's why revalidate slipped through. Sorry about that.

Actually, all revalidate() does is call invalidate(). That in turn causes validate() to be called again when the component is shown again. So validate() is a good alternative.
blingo james
Greenhorn

Joined: Oct 12, 2008
Posts: 25
Originally posted by pete stein:


Not so strange as you are working with AWT and revalidate is a method from the Swing class, JComponent. And this leads me to my question: why AWT and not the more robust Swing? Also, have you considered using CardLayout to swap panels?


I'm learning Java, and my book started with AWT...
But maybe AWT has some advantage with support to small computers?

Will definitely look into CardLayout, thanks.

How do I know that I'm doing things right?
If everything looks o.k that means that all o.k?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Sounds as though your book is out of date.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Originally posted by blingo james:
How do I know that I'm doing things right?
If everything looks o.k that means that all o.k?

Trust me on this: your program is rubbish.

But that's normal - everybody's first program is rubbish
In time, both your skills and style will improve drastically, and so will your programs. When I read back code I've written as late as 2002 I can't even recognize it as my own.
blingo james
Greenhorn

Joined: Oct 12, 2008
Posts: 25
Thanks for the tip,
I'm hoping that some day I will write non-rubbish,
And until that day,
I hope that rubbish can bring income `-).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Well remember: your employer will prefer "working code" over "nice looking code"
"Well-designed code" is somewhere in between (probably closer to the "working code") but if only your user interfaces are written badly (in code that is) I doubt he cares.
Fortunately, when I said your program would be rubbish, I meant user interface code especially. That's always a problematic point.

That and:
- not coding against interfaces / abstract class (e.g. declare as List instead of Vector / ArrayList)
- missing existing API calls and re-implementing those


I still find myself guilty of that second one now and then.
blingo james
Greenhorn

Joined: Oct 12, 2008
Posts: 25
Originally posted by Rob Prime:

That and:
- not coding against interfaces / abstract class (e.g. declare as List instead of Vector / ArrayList)
- missing existing API calls and re-implementing those


I still find myself guilty of that second one now and then.[/qb]


Thanks, will keep those in mind...
[ October 15, 2008: Message edited by: blingo james ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing component