Expandable Text Area in Swing

Using this wonderful thing called WordPress, I noticed that in the post edit screen there is a really neat widget that I had never seen before. It’s basically a text area which is expandable via dragging a small triangle in the bottom right corner. It’s very useful, and I can see many applications of this kind of thing. Mostly its on those GUI forms where you have a miniscule text area where you want to write huge swathes of text. For example, a CV experience text area on a website. Once the text has been pasted in, it is constricted in a tiny scrollpane area with tiny scrollbar handles. Sometimes the text area has its own behaviour in that it will expand as the the text area expands, however I think being able to drag the size of the text area gives a lot more power to the end user experience.

Introducing the Swing Expandable Text Area.. !

I spent a bit of time creating my own Swing version of this component. It behaves much like the wordpress example except that the draggable corner exists hovering on top of the text area, rather than on some border around it. The example I have in my test program uses a GridBaglayout, but it should work fine in any kind of layout. It is important to note that the act of dragging the component resets its size, preferred size and minimum size, effectively overriding size enforcements from whatever layout manager is being used. Other things to note :

  • It is still a little buggy when you have lots of text in the text area; resizing behaves a bit strangely.
  • You need to use it with a scroll pane or the component won’t work.
  • Text is highlighted in some situations when resizing. It doesn’t seem like too much of a problem though.

Here is the code :

And here is my little test program :

Posted in Swing, Swing Components | Tagged , , , , | 3 Comments

3 Responses to Expandable Text Area in Swing

  1. Kyle Howard says:

    Hey, your code chunk worked great for me, thanks! I did have an issue with the root window not re-sizing with the JTextArea though. I was able to get around it with the function:

    public static Window findWindow(Component c) {
    if (c == null) {
    return JOptionPane.getRootFrame();
    } else if (c instanceof Window) {
    return (Window) c;
    } else {
    return findWindow(c.getParent());

    Then I added the function call to your mouseDragged function (above this.repaint):

    Window rootWindow = GeneralGUITools.findWindow(getScrollPane());

    (GeneralGUITools is my class where the findWindow function resides)
    Now if dragging the arrow in the JTextArea causes it to go outside the height or width of the Window, the Window will resize dynamically with it.

    Again, thanks a ton for this class, it was very helpful to me.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">