views:

167

answers:

2

First to describe the problem: During a re-layout due to a change in width or height, Java's ScrollPaneLayout requests the preferredSize of it's children BEFORE setting the bounds of it's viewport. On Text-Components calling getPreferredSpan can change the layout since this getPreferredSpan calls setSize internally. The effect is, that no view (extending javax.swing.text.View) can for sure retrieve the real size of it's parent Component in this situation. A getVisibleRect will fail in this situate as the bounds of the viewport are not yet set. Checking the validity of the text component to block the layout process is not an option as it is already valiid in this scenario.

So, the question is: is there a possibility to find out, whether the bounds of a JComponent are up to date?

+1  A: 

Maybe you could subclass ScrollPanel and set a custom property like "com.mycompany.BoundsSet" to false when invalidated and set it true when the bounds are set. Then the view can check this property and if it's false, revalidate the ScrollPanel, and return the last know preferred size. Hopefully then, the ScrollPane will proceed and set it's bounds, then the revalidate event will come down the pipe, and the flag will have been cleared, allowing you to trust the getBounds(). Or something like that.

Software Monkey
A: 

Can't you simply use Component#isValid method. If the method returns true then it means that the component is correctly sized and positioned within its parentcontainer and all its children are also valid.

Suraj Chandran