Lieuwe
Lieuwe

Reputation: 1840

GridBagLayout anchor & JScrollPane issues

I have the following class

public class Demo {

private JFrame mainFrame;

static public Color BGCOLOUR1 = new Color(240, 240, 240);

public Demo() {
    mainFrame = new JFrame("Demo");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setLayout(new BorderLayout());
    mainFrame.setSize(900, 800);

    JPanel centralPanel = new JPanel();
    centralPanel.setOpaque(true);
    centralPanel.setBackground(BGCOLOUR1);
    centralPanel.setLayout(new GridBagLayout());
    centralPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20),
            BorderFactory.createTitledBorder("Panel")));

    JPanel insidePanel = new JPanel();
    insidePanel.setOpaque(true);
    insidePanel.setBackground(BGCOLOUR1);
    insidePanel.setLayout(new BorderLayout());
    insidePanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Inside panel"),
            BorderFactory.createEmptyBorder(10, 10, 10, 10)));


    JTextArea insidePanelText = new JTextArea(6, 50);
    insidePanelText.setLineWrap(true);

    insidePanel.add(insidePanelText);

    centralPanel.add(insidePanel, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTH,
            GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0));

    JScrollPane scrollPane = new JScrollPane(centralPanel);
    mainFrame.add(scrollPane);
}

Why is the inside panel positioned in the centre of the centralPanel (vertically) when I set the GridBagConstraints anchor to NORTH? I would like it positioned at the top.

Also, if I add the centralPanel in a JScrollPane before adding it to the mainFrame as per the example I can resize the application larger just fine, but as soon as I resize it smaller (even though it is still larger than I originally started it) a scroll bar appears. How can I prevent that from happening?

Edit: To illustrate the scrolling problem (I packed the frame when I took these screens):

The application when started

When I start the application it has no scrollbars

The application when made larger and then smaller again

I resize the window larger, and then smaller again. As soon as I make it smaller, the scrollbar appears.

Upvotes: 1

Views: 507

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

As far as GridBagLayout is concerned, based on the properties you've supplied, it is been laid out to the NORTH of the cell.

GridBagLayout works mostly to the preferred size of the components and calculates the positions of each component around the center of the parent container.

If, instead of:

new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTH,
        GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0)

I use something like:

new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.NORTH,
        GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10, 10), 0, 0)

it will produce the desired result.

Basically, by using gridy and setting it to 1, we're asking GridBagLayout to give all the remaining vertical space to this cell, but because we're not filling the cell, the contents is pushed to the NORTH of the cell

Vertical position

Also, if I add the centralPanel in a JScrollPane before adding it to the mainFrame as per the example I can resize the application larger just fine, but as soon as I resize it smaller (even though it is still larger than I originally started it) a scroll bar appears. How can I prevent that from happening?

I couldn't really replicate this particular problem, I could use either pack or setSize and resized the window smaller to its "packed" size and the scroll bars would appear, but once I resized the window beyond the "packed" size the scroll bars would disappear

Upvotes: 2

Related Questions