Swing Textfield/TextArea Collapse on Dialog Resize

You might be working with Swing’s GridBagLayout and feel a little like …

this guy

because you see something like this :

when all you wanted was this:

This is because you are adding text to JTextArea. This causes a scrollbar to appear, which effectively increases the size of the component… and this really confuses the gridbag manager.

If the text you add ino the text area is only a few words it should look fine, like the second screenshot, but as soon as the text in the text area overshoots, scrollbars appear and the widget collapses.

I won’t go into the complexities of this problem. Needless to say the simple solution is to set the weights to 1.0 on the constraints :

You can reproduce this problem with some code that I have produced. Just uncomment lines 86 and 87. Notice the difference?

A more detailed examination of this problem can be found here :

http://www.coderanch.com/t/334704/GUI/java/GridBagLayout-JScrollPane-Bug-Explained-there


Posted in Swing | Tagged , , , , , , | 1 Comment

One Response to Swing Textfield/TextArea Collapse on Dialog Resize

  1. Fulvio says:

    THANKS a lot! <3
    I had the same annoying problem on a GUI, but for me the collapsing was only vertical, and it was appearing only from time to time, making impossible for me to find out the problem.

    This, however, fixed it!

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="">

THREE_COLUMN_PAGE