resizing a JTextPane dynamically while typing

Hello,

I'm developing an app that calls for maintaining a list of text "segments" that each have a corresponding button. I've created a class called TextSegment (JPanel subclass) that contains a JButton and a JTextPane, and add these dynamically to another JPanel container at the user's request.

Each TextSegment starts off with a fixed height of one line. As the user types, the contents scroll properly, but I'd like to instead automatically grow it vertically (when the user types a CR or the text wraps) so that all the text is visible.

I'm somewhat new to Java/Swing, and am thinking that I should be able to recognize an event where the JTextPane text area grows, and enlarge its container accordingly.

Is this possible? Thanks in advance for any tips or pointers.

[809 byte] By [MMCa] at [2007-11-27 4:50:58]
# 1

You should be able to add a DocumentListener to the JTextPane. You will get notified whenever text is added or removed from the Document. As text is added or removed, the preferred size of the text pane should change. Then you would need to invoke the LayoutManger so it is aware of the change in preferred size. You would do this by invoking the revalidate() method on the parent Container of the JTextPane.

camickra at 2007-7-12 10:04:31 > top of Java-index,Desktop,Core GUI APIs...