Tom T
Tom T

Reputation: 59

jtextarea wont fill panel with GridBagConstraints

From my code I expect my JTextArea to fill the top left border seen below: enter image description here

But as you can see its taking up a tiny section in the middle. I am using GridBagConstraints on the panel which contains the components.

There is a main class which calles up a class called frame. This class creates the JFrame and sets the size as well as other things. This is then called from the MainFrame.java which has extended the jframe which creates 3 panels and sets their layout. this is seen below

import javax.swing.*;
import java.awt.*;

public class MainFrame extends JFrame
{
    private Panel1 storyPanel;
    private Panel2 statsPanel;
    private Panel3 commandsPanel;

    public MainFrame(String title)
    {
        super(title);

        // Setting Layout
        GridBagConstraints gbc = new GridBagConstraints();

        storyPanel = new Panel1();
        storyPanel.setLayout(new GridBagLayout());
        statsPanel = new Panel2();
        commandsPanel = new Panel3();

        Container p = getContentPane();

        p.add(storyPanel, BorderLayout.WEST);
        p.add(statsPanel, BorderLayout.EAST);
        p.add(commandsPanel, BorderLayout.SOUTH);

    }
}

The panel in questions is Panel1 or storyPanel. I have set the layout and the code calls the Panel1.java as seen below:

import javax.swing.*;
import java.awt.*;

public class Panel1 extends JPanel
{
    public Panel1()
    {
        GridBagConstraints gbc = new GridBagConstraints();

        //Set size of Panel1
        int xsizeP1 = (Frame.xsize() / 2);
        int ysizeP1 = (Frame.ysize() / 3 * 2);
        setPreferredSize(new Dimension(xsizeP1, ysizeP1));
        setBorder(BorderFactory.createLineBorder(Color.black));

        //Adding JTextArea and adding settings to it
        JTextArea storyLine = new JTextArea("       test        ");
        storyLine.setLineWrap(true);
        storyLine.setWrapStyleWord(true);
        storyLine.setEditable(false);


        //Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
        JScrollPane scroll = new JScrollPane(storyLine);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        //GridBagConstraints setup for components
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.fill = GridBagConstraints.VERTICAL;
        add(scroll, gbc);
    }
}

I don't understand why my gbc.fill isnt making the JTextArea fill the top left border of the screen shot.

Thanks in advance for any reply's

-Tom T


changing the layout to border layout

import javax.swing.*;
import java.awt.*;

public class Panel1 extends JPanel
{
    public Panel1()
    {
        //GridBagConstraints gbc = new GridBagConstraints();
        //gbc.weightx = 0.1;
        //gbc.weighty = 0.1;
        BorderLayout b = new BorderLayout();

        //Set size of Panel1
        int xsizeP1 = (Frame.xsize() / 2);
        int ysizeP1 = (Frame.ysize() / 3 * 2);
        setPreferredSize(new Dimension(xsizeP1, ysizeP1));
        setBorder(BorderFactory.createLineBorder(Color.black));

        //Adding JTextArea and adding settings to it
        JTextArea storyLine = new JTextArea("       test        ");
        storyLine.setLineWrap(true);
        storyLine.setWrapStyleWord(true);
        storyLine.setEditable(false);


        //Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
        JScrollPane scroll = new JScrollPane(storyLine);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

    //gbc.gridx = 0;
    //gbc.gridy = 0;
        //gbc.weightx = 1;
        //gbc.weighty = 1;
        //gbc.fill = GridBagConstraints.BOTH;
        add(scroll, b.CENTER);
    }
}

Upvotes: 0

Views: 581

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347214

  1. Don't set the layout of storyPanel, as it's contents have already been added and laid out, so changing the layout manager here will discard any properties you applied. Instead, set the layout in Panel1's constructor before you add any components.
  2. Use GridBagConstraints.BOTH for the fill property. The fill property can only have one specified value
  3. Use weightx and weighty to specify how much of the available space the component should use

For example...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MainFrame extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new MainFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private Panel1 storyPanel;
//  private Panel2 statsPanel;
//  private Panel3 commandsPanel;

    public MainFrame(String title) {
        super(title);

        storyPanel = new Panel1();

        Container p = getContentPane();

        p.add(storyPanel, BorderLayout.WEST);
        p.add(new JLabel("East"), BorderLayout.EAST);
        p.add(new JLabel("South"), BorderLayout.SOUTH);

    }

    public class Panel1 extends JPanel {

        public Panel1() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            //Set size of Panel1
            setBorder(BorderFactory.createLineBorder(Color.black));

            //Adding JTextArea and adding settings to it
            JTextArea storyLine = new JTextArea(20, 20);
            storyLine.setLineWrap(true);
            storyLine.setWrapStyleWord(true);
            storyLine.setEditable(false);

            //Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
            JScrollPane scroll = new JScrollPane(storyLine);
            scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

            //GridBagConstraints setup for components
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.weighty = 1;
            add(scroll, gbc);
        }
    }
}

Having said all that, a BorderLayout would be simpler

public class Panel1 extends JPanel {

    public Panel1() {
        setLayout(new BorderLayout());

        //Set size of Panel1
        setBorder(BorderFactory.createLineBorder(Color.black));

        //Adding JTextArea and adding settings to it
        JTextArea storyLine = new JTextArea(20, 20);
        storyLine.setLineWrap(true);
        storyLine.setWrapStyleWord(true);
        storyLine.setEditable(false);

        //Adding JScrollPane to the JTextArea and making it have a vertical scrollbar
        JScrollPane scroll = new JScrollPane(storyLine);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        add(scroll);
    }
}

i tried a border layout and set it to center expecting it to resize but that failed as well

Would suggest that you're making a fundamental mistake some where, as it works fine for me. Remember, set the layout BEFORE you add any components to the container

Upvotes: 3

Related Questions