This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Text canvas vertical scroll" Watch "Text canvas vertical scroll" New topic
Author

Text canvas vertical scroll

Dmitry Danilov
Ranch Hand

Joined: Jan 27, 2004
Posts: 38
Hello,

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
Wow!..
This has to be digested...
Thanks a lot for the reply, Craig!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Text canvas vertical scroll
 
Similar Threads
Please help with scope of panels...
ScrollPane trouble
Line spacing
paint() and performance
North Panel Not Displaying....