views:

284

answers:

1

In the following example, I want to be able to expand the text area when I maximize or minimize the window. Right now, the textarea is set to the cols/rows. If I hit maximize, the text area should expand as the window expands.

Note: this is kind of a pseudo example. I may add more components which is why I used the GridBagLayout manager.

The version below gives me what I want:

package org.berlin.pino.test.functional.jogl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class BasicText {

    public static JPanel buildPanel() {

        final JPanel panel = new JPanel(new GridBagLayout());
        final GridBagConstraints gc = new GridBagConstraints();

        final JTextArea text = new JTextArea("Text");
        final JScrollPane scrollPane = new JScrollPane(text); 
        gc.fill = GridBagConstraints.BOTH;
        gc.weightx = 1;
        gc.weighty = 1;        

        // Add the textarea -> scroll pane -> to the panel -> to the jframe                       
        panel.add(scrollPane, gc);        
        return panel;
    }

    public static void main(String[] args) {


        JFrame frame = new JFrame("Hello World!");
        frame.setLayout(new GridBagLayout());
        final GridBagConstraints gc = new GridBagConstraints();
        gc.fill = GridBagConstraints.BOTH;
        gc.weightx = 1;
        gc.weighty = 1;

        frame.add(buildPanel(), gc);
        frame.setPreferredSize(new Dimension(300, 300));
        frame.setLocation(200, 100);
        frame.setBackground(Color.white);               

        frame.pack();
        frame.setVisible(true);
    }

} // End of the class //
+1  A: 

On your GridBagConstraints use

gc.weightx = 1d;
gc.weighty = 1d;

JavaDoc for weightx/weighty says:

Specifies how to distribute extra horizontal/vertical space.

Peter Lang
Thanks, that worked.
berlinbrown2
@berlinbrown2: Glad I could help :) BTW: If your problem is solved, you can accept my answer.
Peter Lang