• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

validate revalidate !!

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am perplexed at the behavior of the following example! I am resizing a button and see it change it's size. Perplexing thing is that validate() works but not revalidate()! repaint doesn't seem to be necessary!

Also, what do I need to do have the frame resize itself to showcomplete buttons?

There seems to be a lot of confusion about use of validate, revalidate, invalidate, paint, repaint! Is there an authoritative reference on this.

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a tough time remembering what goes with what also. Paint and repaint deal with the graphics class, so in your example, they won't do anything.

Validate makes sure that the component has a valid layout. The API's are a little vague on invalidate, but my assumption (probably incorrect) would be that it removes the layout of the component.

Revalidate, IIRC, has to do with handling component changes due to some kind of an event...as in a button says "on", and then when you click it, it says "off", etc.

Pack resizes your window to use each components preferred size. I think if you remove your validate and repaint statements, the frame should size properly so that your components fit.

I also find it useful when I get confused to use the glossary from here:
http://mindprod.com/jgloss/

It's interesting that you used show, and not set visible. I thought show was depricated, but I just discovered that JFrame inherits the show method of the Window class which is not deprecated..
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be easier to see the effects if you set a size for your JFrame. If you use 'pack' you'll have to call it again to properly resize the frame after each component size change.

'revalidate' is a JComponent method and works well for swing use. I call it after any changes that may affect a Containers layout, eg, adding/removing/resizing components, changing borders or fonts. 'revalidate' will also reset scrollbars on a parent JScrollPane.

'validate' is a Component method that is overridden in Container. You can use it in swing instead of 'revalidate'. It is useful in AWT apps the way 'revalidate' is in swing.

'invalidate' is not often needed, mostly (I think) just before 'validate' to mark a (changed) component for the attention of a parent ScrollPane.

'repaint' may or may not be required after 'revalidate' or 'validate', you just have to experiment to see what is needed in each situation. I try to use the minimum required.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About the app — 'setSize' is not so useful for resizing components in swing. 'setPreferredSize' works much better in swing and, as of j2se 1.5, is now also a Component (AWT) method. It is a luxury in gui work.

I added some things to your app as a way of exploring these validation and sizing options. 'Button 1' works with 'setPreferredSize' and 'Button 2' with 'setSize'. Observe that the 'setSize' method changes the size but not the location of button2.
 
Anne Forumer
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Craig for going through this effort to help!

I'm perplexed even more now! works - and doesn't if you don't check . Situation gets reversed if you or !

I am under the impression that forces everything to preferredSize.

It is good to be try out different options. Can you recommend a good reference which can help to make more sense?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This page in the tutorial may be helpful How Layout Management Works. Otherwise, I don't know of any references to recommend. This seems to be an area best learned through practice and experimentation. Curiosity and looking at how others have put things together seem to help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic