|
Replies:
8
-
Last Post:
Aug 27, 2009 11:06 AM
by: himanshu_ranavat
|
Threads:
[
Previous
|
Next
]
|
|
|
|
|
|
When to call validate?
Posted:
Oct 30, 2007 4:33 PM
|
|
|
Hello,
I am working on a half-object based GUI Library which allows to write Swing-Like code on the server, and the user-interface gets "projected" to the clients applet/webstart client.
The server sends down a stream of instructions (like JFrame.add(....)) which are interpreted on the EDT using: EventQueue.invokeLater(new CommandListInterpreter(this, list)). This does nothing more than to call some AWT/Swing methods suing reflection on the EDT.
However the problem I have is that it seems I have to call validate on the root-container every time I modify the GUI, otherwise it does not work. However when modifying the GUI in the EDT as an reaction to an event (using swing, not my library) I don't have to validate anything - it seems Swing does it automatically. I am a bit in doubt when/where to call validate on which objects?
Any hints would be very welcome, lg Clemens
PS: The library is/will be opensource, as soon as I think it could be useful I release the source plus some demos. Lets kill Ajax
|
|
|
|
|
|
|
Re: When to call validate?
Posted:
Oct 30, 2007 5:04 PM
in response to: linuxhippy
|
|
|
Whenever you modify a component hierarchy, i.e. remove or add a component then yes you do need to revalidate generally. This is preferable to validate() since it posts an event to the EDT which gets processed in the normal order, also revalidate() is thread-safe like repaint(). I haven't looked into when swing automatically revalidates. When you change most bound properties in Swing like setting the text on a JLabel or anything like that, then it generally WILL revalidate()/repaint(), but base operations like adding components to panels and things require a manual validation call. The idea being that if you're doing a lot in one go its stupid revalidating after each new component.
|
|
|
|
|
|
|
|
Re: When to call validate?
Posted:
Oct 31, 2007 12:08 AM
in response to: fred34
|
|
|
Hi fred,
Thanks for your answer, unfortunately it still leaves some question open:
1.) When modifying some sub-components in e.g. a JFrame, is it enough/a good idea to call invalidate/validate on the JFrame itself? Isn't this inefficient? 2.) How is the "atomatic" validation, which is done in Swing, realized? 3.) Do I need to call invalidate()?, is validate() enough or should I better call revalidate() when I am using Swing components.
Thanks a lot in advance, lg Clemens
|
|
|
|
|
|
|
|
|
|
Re: When to call validate?
Posted:
Oct 31, 2007 5:42 AM
in response to: swv
|
|
|
@swv: Thanks for your links, but they all point to custom painting which I already know about. Furthermore I don't see how custom painting influces the validation process.
What I wonder is when/how does Swing itself call invalidate/validate. As far as I know when I add a new JButton to a JPanel inside the EDT called by e.g. a mousePressed() I don't need to call revalidate, right? So it seems there's some logic which seems to take care which components need to be validated atomatically.
However my application does all UI-Operations no triggered by events, but triggered by another thread using invokeLater(). So when/where and on which obejcts should I call validate()?
Thank you in advance, lg Clemens
|
|
|
|
|
|
|
|
Re: When to call validate?
Posted:
Oct 31, 2007 10:04 AM
in response to: linuxhippy
|
|
|
swv is correct. As I hinted at in the first reply, many many Swing components will automagically call revalidate()/repaint() in response to a change in a property such as text in a JLabel or JButton or adding components to a JScrollPane (to ensure the scrollbars are the correct size). While custom painting isn't related to validation, when something fundamental changes about a component, then it may need to change its size as well as repaint, hence if you hunt through Swing source code there are many places where revalidate()/repaint() are called sequentially to update a GUI.
If you add a JButton to a JPanel, to DO need to call revalidate(). Component addition/removal isn't handled by Swing as far as I can tell so no automatic revalidation will happen. For performance, make all the changes, then call revalidate().
revalidate() is thread safe. Call it from anywhere and it will post an operation to the EDT.
|
|
|
|
|
|
|
|
Re: When to call validate?
Posted:
Oct 31, 2007 9:53 AM
in response to: linuxhippy
|
|
|
1) Use revalidate() to cause a validation of components to happen on the EDT. This internally calls invalidate() to invalidate that container and sub-containers and then queues an operation to re-layout that container and all subcontainers. You don't need to call invalidate()/validate() separately in Swing. This differs from AWT where upon adding/removing/changing a component size, you need call invalidate()/validate() upon the container things have been added to. From the API:
<pre>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. </pre>
Call revalidate() on the component that has been changed in some way. Anything that needs updating will then be taken care of as the internal code checks the component hierarchy to determine this.
2) Look in the source code for pack() in JFrame/Window/Frame etc. Its in one of those. In AWT/Swing upon first display, pack() is responsible for calling necessary methods to hook windows to native peers and calling the layout manager to set the sizes and positions of all components in the hierarchy. It's during this process that validation is done by the layout manager calling various getPreferredSize and getXXXSize() methods on all components.
3) In Swing, generally always use revalidate()
|
|
|
|
|
|
|
|
Re: When to call validate?
Posted:
Oct 31, 2007 3:01 PM
in response to: linuxhippy
|
|
|
You should put a break point in the invalidate() method of your top-level components (windows, main content panels, etc) and make sure invalidate() is being called when you execute a new instruction.
|
|
|
|
|
|
|
|
Re: When to call validate?
Posted:
Aug 27, 2009 11:06 AM
in response to: jvaudry
|
|
|
Wouldnt I rather call validate() if I want to just reposition and repaint a part (say a custome JComponent added to a JPanel) of a JPanel. Calling revalidate, will cause invalidate to be called all the way up to the JPanel and then validation will occur down to all children for the JPanel. When I actually want to validate only one child of the JPanel. Isnt this inefficient?
|
|
|
|
|