File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Text canvas vertical scroll Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Text canvas vertical scroll" Watch "Text canvas vertical scroll" New topic

Text canvas vertical scroll

Dmitry Danilov
Ranch Hand

Joined: Jan 27, 2004
Posts: 38

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.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Dmitry Danilov
Ranch Hand

Joined: Jan 27, 2004
Posts: 38
This has to be digested...
Thanks a lot for the reply, Craig!
jQuery in Action, 3rd edition
subject: Text canvas vertical scroll
It's not a secret anymore!