• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComponent.revalidate()

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"
TextFields
One column is the expected width of on e character in the font you are using for the text.. you are supposed
to specify n (number of columns) as the column width.
Users tend to find scrolling text fields irritating, so you should size the fields generously.

...

After changing the size of a text box with the setColumns method, call the revalidate
method of the surrounding container.
textField.setColumns(10);
panel.revalidate();
The revalidate method recomputes the size and layout of all components in a container.
After you use the revalidate method, the layout manager resizes the container, and the
changed size of the text field will be visible.

The revalidate method belongs to the JComponent class. It doesn’t immediately resize the
component but merely marks it for resizing. This approach avoids repetitive calculations
if multiple components request to be resized. However, if you want to recompute all
components inside a JFrame, you have to call the validate method—JFrame doesn’t extend
JComponent.
"

Core Java Volume 1 (8th Edition) Pg. 378.

Does the author refer to Container.validate() ?

On the other hand, the documentation for JComponent.revalidate() reads as follows:

"revalidate

public void revalidate()
Supports deferred automatic layout.
Calls invalidate and then adds this component's validateRoot to a list of components that need to be validated. Validation will occur after all currently pending events have been dispatched. In other words after this method is called, the first validateRoot (if any) found when walking up the containment hierarchy of this component will be validated. By default, JRootPane, JScrollPane, and JTextField return true from isValidateRoot.

This method will automatically be called on this component when a property value changes such that size, location, or internal layout of this component has been affected. This automatic updating differs from the AWT because programs generally no longer need to invoke validate to get the contents of the GUI to update."Source: Java 1.6 API

I have noticed that leaving the columns property to [0] when using NetBeans GUI to create a demo seemed to have no effect, since there seems to be a default width for JTextField:








 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jon. First of all, several of your threads have been moved to the GUI forum. I will move this one as well. The next time you have a Swing related question, please post it in the GUI forum directly.

Regarding this topic, what is your question specifically?
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Hi Jon. First of all, several of your threads have been moved to the GUI forum. I will move this one as well. The next time you have a Swing related question, please post it in the GUI forum directly.

Regarding this topic, what is your question specifically?


Oh right sorry, well I am confused as to whether to use Container.validate() when the program design requires a refresh of the position and size of the Container which might include one or more Components, and, use JComponent.revalidate() to refresh the size and position of just a component.

 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
revalidate() is a method in JComponent, not Component.

Always use revalidate(). The situation that you actually have to lay out all the components in the entire frame will be rare, and when it does occur, you can easily use a JPanel as the content pane, and call revalidate() on that panel.
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:I am confused as to whether to use Container.validate() when the program design requires a refresh of the position and size of the Container which might include one or more Components, and, use JComponent.revalidate() to refresh the size and position of just a component.


Did you read and understand the matter you posted above?
Jon Camilleri wrote:After changing the size of a text box with the setColumns method, call the revalidate
method of the surrounding container.
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:The situation that you actually have to lay out all the components in the entire frame will be rare, and when it does occur, you can easily use a JPanel as the content pane, and call revalidate() on that panel.


And that would likely be a situation in which calling pack() on the frame would be a better option.
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot about pack()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!