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 :

			JScrollPane sp = new JScrollPane(ta);

				gbc.weightx = 1.0;
				gbc.weighty = 1.0;
				gbc.fill = GridBagConstraints.BOTH;

				dialog.add(sp, gbc);

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

package dialogtable;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

 * Test Class
public class Test extends JFrame{

	JFrame mainFrame = new JFrame();

	public static void main(String[] args) {

		Test m = new Test();

	private void run() {

		JPanel panel = new JPanel();

		JButton button = new JButton("PRESS");

		button.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {

				final JDialog dialog = new JDialog();

				dialog.setLayout(new GridBagLayout());

				GridBagConstraints gbc = new GridBagConstraints();
				gbc.insets = new Insets(2,2,2,2);

				int row = 0;

				gbc.gridx = 0;
				gbc.gridy = row;
				gbc.anchor = GridBagConstraints.NORTHWEST;
				gbc.fill= GridBagConstraints.HORIZONTAL;

				dialog.add(new JLabel("Author : " ), gbc);
				gbc.gridx = 1;

				JTextField tf = new JTextField("Johann Wolfgang von Goethe");

				dialog.add(tf, gbc);

				///increment row

				gbc.gridx = 0;
				gbc.gridy = row;
				gbc.anchor = GridBagConstraints.NORTHWEST;
				gbc.fill= GridBagConstraints.HORIZONTAL;

				dialog.add(new JLabel("Description : " ), gbc);
				gbc.gridx = 1;

				JTextArea ta = new JTextArea(5, 20);
				ta.setText("Desc : ");

				JScrollPane sp = new JScrollPane(ta);

				//gbc.weightx = 1.0;
				//gbc.weighty = 1.0;
				gbc.fill = GridBagConstraints.BOTH;

				dialog.add(sp, gbc);


				ta.setText("Johann Wolfgang von Goethe, geadelt 1782 (* 28. August 1749 in Frankfurt am Main, HRR; † 22. März 1832 in Weimar, Sachsen-Weimar-Eisenach), war ein deutscher Dichter. Er forschte und publizierte außerdem auf verschiedenen naturwissenschaftlichen Gebieten. Ab 1776 bekleidete er am Hof von Weimar unterschiedliche politische und administrative Ämter." +
						"Goethes literarische Produktion umfasst Gedichte, Dramen, erzählende Werke (in Vers und Prosa), autobiografische, ästhetische, kunst- und literaturtheoretische sowie naturwissenschaftliche Schriften. Auch sein umfangreicher Briefwechsel ist von großer literarischer Bedeutung. Goethe war ein Vorreiter und der wichtigste Vertreter des Sturm und Drang. Sein Roman Die Leiden des jungen Werthers machte ihn 1774 in ganz Europa berühmt. Später wandte er sich inhaltlich und formal den Idealen der Antike zu und wurde ab den 1790er Jahren, gemeinsam mit Friedrich Schiller und im Austausch mit diesem, zum wichtigsten Vertreter der Weimarer Klassik. Im Alter galt Goethe auch im Ausland als Repräsentant des geistigen Deutschland." +

				///increment row

				gbc.gridx = 1;
				gbc.gridy = row;
				gbc.fill = GridBagConstraints.NONE;

				final JButton button = new JButton("OK");
				button.addActionListener(new ActionListener(){

					public void actionPerformed(ActionEvent e) {

				gbc.anchor = GridBagConstraints.EAST;
				dialog.add(button, gbc);



		panel.add(new JScrollPane(button));



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


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> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>