Resize JTextPane height to fit text (fixed width)?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Earl_Williams
Posted On:   Thursday, April 19, 2001 11:14 AM

I would like to show user-editable text in a fixed-width box, and resize the box height during editing in order to show all the text. Seems like a simple and common thing to do, but I've had no luck despite doing the commonsense stuff and trying every hack I could find by searching the JDC Forums and JGuru. Here's what I know (or think I know): 1. I need to put a JTextPane (user-editable text) in a JScrollPane (so the size of the JTextPane will not be limited by a containing parent object). 2. I need to resize the JScrollPane in the insertUpdate and removeUpdate event handlers of a DocumentListener registered on the JTextPane, so that the JScrollPane shows the full JTextPane (no scrollbars). 3. Calling getPreferr   More>>

I would like to show user-editable text in a fixed-width box, and resize the box height during editing in order to show all the text. Seems like a simple and common thing to do, but I've had no luck despite doing the commonsense stuff and trying every hack I could find by searching the JDC Forums and JGuru.

Here's what I know (or think I know):

1. I need to put a JTextPane (user-editable text) in a JScrollPane (so the size of the JTextPane will not be limited by a containing parent object).

2. I need to resize the JScrollPane in the insertUpdate and removeUpdate event handlers of a DocumentListener registered on the JTextPane, so that the JScrollPane shows the full JTextPane (no scrollbars).

3. Calling getPreferredSize on the JTextPane usually gives me the size I need, _but not always_. In particular, the preferredSize does not appear to be recalculated when the text is reduced (via Delete, or Pasting text when a larger amount of text is already selected). And calling setPreferredSize(null) doesn't appear to trigger the appropriate recalculation either.


Any ideas would be greatly appreciated, particularly if accompanied by sample code. This should be trivial, so I must be missing something easy. Thanks very much.

   <<Less
About | Sitemap | Contact