aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Am I using repaint correctly? 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 "Am I using repaint correctly?" Watch "Am I using repaint correctly?" New topic
Author

Am I using repaint correctly?

Larry Homes
Greenhorn

Joined: Jan 18, 2009
Posts: 25
Hello

I don't have much gui experience so I am not sure I am using repaint correctly in this case. Here is some example code to demonstrate:



When repaint is called, the applet container just gets blanked instead of displaying the new panel, and then, after some waiting, shows the new Panel. But if, while the screen was blanked, if I force a repaint by minimizing and restoring, the new panel is show immediately. This leads me to beleive I am using repaint wrong. Any suggestions?


Thanks
Larry Homes
Greenhorn

Joined: Jan 18, 2009
Posts: 25
Well I solved it.

Here is my solution if anyone is interested. I called invalidate(), then validate() before the repaint. So my actionPerformed methods looked something like:



I am still interested as to why this works and why just repaint() did not though. I just found this solution on another forum, so if anyone has an answer, I'd still like to hear it.

Thanks
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
All repaint() does is repaint the components at there current size and location.

When you create a component is has a 0 size and location. The size and location is only determined once the layout manager has been invoked. Adding a component does not invoke the layout manager since you could be adding multiple components so it doesn't make sense to invoke the layout manager every time.

So generally all you need to do us use the validate() method (although I prefer to use revalidate() on Swing components). This will basically invoke the layout manager. In many cause this will also cause the panel to be repainted, but it not you can always add the repaint() to make sure.

In your case you should also look at using a CardLayout which allows you so swap panels easily and look after all the details behind the scenes.
Larry Homes
Greenhorn

Joined: Jan 18, 2009
Posts: 25
Hi

Thanks for the answer. I did look into cardlayout. Here is what I got:



While that works, is there a more elegant way to get the CardLayout using panel than a string of getParents()?


Thanks for the help
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Pass in the CardLayout as a parameter to the child panels when you create them.
Larry Homes
Greenhorn

Joined: Jan 18, 2009
Posts: 25
If I pass in the CardLayout, I would still need the panel instance it belongs to for the show() method. I couldn't find a way to get the owning component of a layout, but passing in the panel that uses CardLayout works just as well. So the constructor would be



and the switching in the actionlistener would be



Thanks for all your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Am I using repaint correctly?