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


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

validate revalidate !!

Anne Forumer
Ranch Hand

Joined: Apr 19, 2004
Posts: 72
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.

C. Alan
Ranch Hand

Joined: Dec 17, 2004
Posts: 30
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..


<a href="http://www.security-forums.com/forum/viewforum.php?f=48" target="_blank" rel="nofollow">Malware Removal</a> - Get your system running like new again.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Apr 19, 2004
Posts: 72
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

Joined: Jan 14, 2004
Posts: 1535
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validate revalidate !!
 
Similar Threads
How to get a panel to display a button and a component?
how to center this button 1
problem painting
Help on Tooltip
JTabbedPane with Keyevents