• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing component

 
blingo james
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
blingo james
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome thanks.
 
blingo james
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think revalidate() needs to be called as well.
 
blingo james
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds as though your book is out of date.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic