• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Am I using repaint correctly?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3288
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3288
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pass in the CardLayout as a parameter to the child panels when you create them.
 
Larry Homes
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic