This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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.
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.
Joined: Jan 14, 2004
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.
Joined: Apr 19, 2004
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?
Joined: Jan 14, 2004
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.