xXJinXx
xXJinXx

Reputation: 35

Why JTextArea is small?

I try to make a simple app in SWING: using BorderLayout layout on the JFrame, i put on SOUTH an executing button, on WEST a panel that contains a combobox and on EAST a panel that contains 2 JTextAreas. The problem is, both JTextArea are damn small. Any help and explanation will be welcomed.

This is the code for the panel with the 2 text areas

package cipher;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;

class TextPanel extends JPanel {

    private JTextArea inputArea, outputArea;

    public TextPanel() {

        initSize();
        initTextArea();
        initBorder();
        initLayout();

        packing();

    }

    private void packing() {

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.weighty = 1;
        add(inputArea,gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1;
        gbc.weighty = 1;
        add(outputArea,gbc);

    }

    private void initBorder() {

        Border outer = BorderFactory.createEmptyBorder(5, 5, 5, 5);
        Border inner = BorderFactory.createTitledBorder("Text");
        setBorder(BorderFactory.createCompoundBorder(outer,inner));

    }

    private void initLayout() {

        setLayout(new GridBagLayout());

    }

    private void initTextArea() {

        inputArea = new JTextArea();
        inputArea.setPreferredSize(new Dimension(385,400));

        outputArea = new JTextArea();
        outputArea.setPreferredSize(new Dimension(385,400));

    }

    private void initSize() {

        Dimension size = getPreferredSize();
        size.width = 390;
        setPreferredSize(size);

    }

}

I've tried using setSize(x,y) but without success. I've tried using JTextArea(rows,columns) but without success. I've used even setPreferredSize with a Dimension but no succeed.

screenshot of results

Upvotes: 0

Views: 563

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

The probable cause of your issue is the container area is smaller than the preferred size of the text area, GridBagLayout will then default to the minimum size instead.

This is a good example of why you should avoid setting these properties directly and instead make use of the layout manager and the components properties.

To start with, make use of the JTextArea's column and rows properties. This will make a better "guess" at the amount of space it needs to display text to fit within these confines.

Second, use GridBagConstraints#fill to override GridBagLayout and force it to make use of the available space

Fill the space

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TextPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TextPanel extends JPanel {

        private JTextArea inputArea, outputArea;

        public TextPanel() {

            initTextArea();
            initBorder();
            initLayout();

            packing();

        }

        private void packing() {

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(inputArea, gbc);
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(outputArea, gbc);

        }

        private void initBorder() {

            Border outer = BorderFactory.createEmptyBorder(5, 5, 5, 5);
            Border inner = BorderFactory.createTitledBorder("Text");
            setBorder(BorderFactory.createCompoundBorder(outer, inner));

        }

        private void initLayout() {

            setLayout(new GridBagLayout());

        }

        private void initTextArea() {

            // The borders are just here so you can see the different text areas
            inputArea = new JTextArea(10, 20);
            inputArea.setBorder(new LineBorder(Color.BLACK));
            outputArea = new JTextArea(10, 20);
            outputArea.setBorder(new LineBorder(Color.BLACK));

        }

    }
}

I'd also change...

inputArea = new JTextArea(10, 20);
inputArea.setBorder(new LineBorder(Color.BLACK));
outputArea = new JTextArea(10, 20);
outputArea.setBorder(new LineBorder(Color.BLACK));

and make use of JScrollPanes instead of LineBorder

Upvotes: 2

Related Questions