Vertical Align of GridBagLayout Panel on BorderLayout.CENTER
Asked Answered
Y

3

5

What I'm trying to do is place a GridBagLayout Panel on the center of my BorderLayout and vertical align the GridBagLayout panel ( /and text on it ) to the TOP ( because it automaticly puts it in the middle, horizontally AND vertically ).

So what I basicly tried ( but ended up having the text of the GridBagLayout still in the total middle of the page instead of in the middle x and top y):

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.imageio.*;
import javax.swing.BorderFactory;
import javax.swing.border.*;
import java.awt.event.*;

public class Test extends JApplet implements MouseListener, ActionListener {

 public void init() {
    //create borderlayout
    this.setLayout(new BorderLayout());
    //create a GridBagLayout panel
    JPanel gb = new JPanel(new GridBagLayout());
    JLabel content = new JLabel("Some text");
    //set GridBagConstraints (gridx,gridy,fill,anchor)
    setGBC(0, 0, GridBagConstraints.VERTICAL, GridBagConstraints.NORTH);
    gb.add(content, gbc); //gbc is containing the GridBagConstraints
    this.add(gb, BorderLayout.CENTER);
  }

}

So I tried to use the gridbagconstraints anchor to set the alignment vertically to the north, top, but that seems not to work. I also tried to resize the GridBagLayout panel itself ( to make it have full height of the layout, 100%, using panel.setSize and setPreferredSize ) and then vertically align the elements on it using the gridbagconstraints.anchor, but that didn't work either.

Can anyone help me out on this?

Thanks in advance,

Best Regards, Skyfe.

So my question is

Yore answered 9/11, 2010 at 15:56 Comment(2)
I still don't get why this would not be a SSCCE ( pscode.org/sscce.html ) it's short, I only displayed the code that concerns the problem, and it's correct and I stated the problem with the given code so seems like a "SSCCE" to meYore
If it's that it needs to be able to be copy-pasted, just copy-paste it in the init method of a basic applet code or in the main method of a normal java application code.Yore
B
14

Take a careful look at the Javadoc of each property of the GridBagConstraints class before using it.

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

public class Test extends JFrame {

    public static void main(String arg[]) {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        JPanel gb = new JPanel(new GridBagLayout());
        JLabel content = new JLabel("Some text");

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTH;
        gbc.weighty = 1;

        gb.add(content, gbc); // gbc is containing the GridBagConstraints
        frame.add(gb, BorderLayout.CENTER);

        frame.setVisible(true);
    }

}
Bitthia answered 9/11, 2010 at 22:24 Comment(4)
Thanks I understand, and the code you rovided works great! It only works when I set fill = GridBagConstraints.NONE instead of VERTICAL, what's the reason for that?Yore
When setting gbc.fill = GridBagConstraints.VERTICAL, the layout resizes the JLabel to fill all the available vertical space. By default, the characters of a JLabel are centered in its space. So, what you need to do is: JLabel.setVerticalAlignment(SwingConstants.TOP);Bitthia
I think extending JFrame is unnecessary in this example.Intersperse
Agree with @fachexot, the class does not need to extend JFrame, it was an oversight.Bitthia
A
3

In addition to setting fill to NONE, also set weighty to a non-0 value (e.g. 1.0). This tells the layout to allocate all extra space in the GridBag column to that particular cell, but since the label itself is anchored to the north and set to not fill, the extra space will be allocated beneath the label.

Ats answered 9/11, 2010 at 22:16 Comment(1)
Thanks for the explanation! I understand much better what those properties do nowYore
A
1

Your

setGBC(0, 0, GridBagConstraints.VERTICAL, GridBagConstraints.NORTH);

is making the component ("content") filling gb vertically. This may be why you think your component(content) is still in the middle. Try setting it to GridBagConstraints.NONE. This will align your content to the top inside the panel that has GridBagLayout.

for the outer component, you can always use something like:

panel.add(button, BorderLayout.PAGE_START);

and see if it works.

Andreeandrei answered 9/11, 2010 at 16:15 Comment(1)
Thanks for your response, so if I understand it correctly it fills up the GridBagLayout Panel vertically and alligns the components inside of it to the north, but puts the GridBagLayout Panel itself still in the middle of the borderlayout (x- and y-wise). Isn't there a way to align the GridBagLayout panel inside the BorderLayout.CENTER position vertically? Or to make it fill the BorderLayout.CENTER vertically fully? (GridBagConstraints.NONE cause the empty to be white, empty btw )Yore

© 2022 - 2024 — McMap. All rights reserved.