I have an application that draws strings of text on a canvas doing word wrapping and breaking whenever necessary. Canvas' height is initially zero and grows as strings are drawn onto it. The final canvas' height is calculated as a last step of the paint() method. Vertical text scrolling has to be implemented as well, for which purpose I'm using ScrollPane. I'm experiencing one problem here: it looks like I cannot dynamically set the canvas' height by the time all strings have been drawn on it. The getPreferredSize() method is called by the layout manager when the canvas class is instantiated and overriding the method doesn't help much. The only way to make scrolling work is to resize the application frame as soon as it's created at least one pixel in any direction. Then apparently the getPreferredSize() method is called again and the updated canvas' size is set. Using setSize() explicitly doesn't help for some reason... Resizing the frame seems to fire some hidden underlying mechanism which sets the updated size of the canvas and let the scrolling work perfectly... Another way to make it work is to set preferred size to a constant value, eg.
Both workarounds are obviously very weird and cannot be used seriously.
I'm totally stuck here, tried hundreds of different tricks, but still can't help it...
Anybody ever experienced something like this?
P.S. Main app frame class extends Frame, text canvas class extends Canvas. Default layout manager (BorderLayout) is used to layout the ScrollPane with text canvas.