14

I'm new to Java Swing and I have been struggling to start the GridBagLayout from top left corner so that c.gridx=0 c.gridy=0 will put my object on the top left corner.

I'd appreciate if you could help me by telling what I need to do after this point:

    JPanel panel = new JPanel(new GridBagLayout());
    frame.add(panel);
    GridBagConstraints c = new GridBagConstraints();

I know that I have to use NORTHWEST or FIRST_LINE_START constants, but I don't know how. I tried to do it this way' but it did not realize the constants.

    frame.getContentPane().add(panel, BorderLayout.NORTHWEST);

Thanks for your help.

Emir
  • 762
  • 2
  • 8
  • 22

6 Answers6

16

Read the section from the Swing tutorial on How to Use GridBagLayout. The secton on "weightx,weighty" states:

Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks for clarification; Mark's post helped me understand that. – Emir Jun 16 '11 at 06:14
  • This is exactly what I needed--thanks. I didn't wan to have to play games with the layout of the panel within the frame. And this Just Worked with the addition of a few extra lines with my GridBagConstraints setup. Excellent. – tsm Jul 05 '11 at 21:10
14

You need to use your GridBagConstraints' anchor property. This should do it for you:

frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
frame.add(panel, gbc);

I'm not guaranteeing that you won't have to set other properties of the constraints object to get the layout you desire. In particular, you may need to set weightx and weighty to be 1 so that the panel takes up all of the available space given to it.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • @Emir: You can do that, although I find it's usually nice to keep the panel in a separate class than the frame that holds it. If you do that, you don't even really need to extend JFrame, you just need to add the panel to a straight-up JFrame. If you do use `frame.add()`, `frame.setLayout()`, etc what that is doing is delegating the calls to a special panel called the "content pane". – Mark Peters Jun 15 '11 at 21:19
  • @MarkPeters Can this be done without a Frame? I have my GridBagLayout imbedded inside a DeckLayout, and the Frame wouldn't really work for me. ANY help you can provide would be great. I am getting a little tired or playing around with Layouts (I have the exact problem as the original poster.) – john_science Jul 15 '12 at 00:49
  • @theJollySin: GridBagLayout will work in any JPanel, just construct the panel with a `new GridBagLayout()`. How they interact might be a little tricky. If you run into trouble, please post a new question. – Mark Peters Jul 16 '12 at 13:51
6

a quick and simple way:

add a blank JLabel to end of page:

    // your code goes here
    gbc.weightx = 1;
    gbc.weighty = 1;

    bg.add(new JLabel(" "), gbc);  // blank JLabel
mrd abd
  • 828
  • 10
  • 19
  • Worked perfectly. Note: it is importante to use gbc.weightx=1 and gbc.weighty=1 just in the end of the code, right before adding the blank JLabel. So it will not apply for all the components, but just for the blank JLabel. – viniciussss Apr 28 '16 at 15:58
5

For those, who use IDE (e.g. NetBeans), I finally found nice trick: if you want to add components to top and use their preferred sizes: add another empty panel with weighty = 1.0. Copied from auto-generated code (NetBeans):

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.weighty = 1.0;
jPanelOptions.add(jPanelFiller, gridBagConstraints);
peenut
  • 3,366
  • 23
  • 24
5

There's a workaround. You can put the GridBagLayout panel in a BorderLayout panel with BorderLayout.NORTH. Then Component in GridBagLayout panel will start from top position.

static void test4(){
    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(480, 360);

    JPanel borderLayoutPanel=new JPanel(new BorderLayout());
    JPanel gridBagLayoutPanel = new JPanel(new GridBagLayout());
    borderLayoutPanel.add(gridBagLayoutPanel, BorderLayout.NORTH);
    frame.add(borderLayoutPanel);

    JButton testButton=new JButton("test button");
    GridBagConstraints c = new GridBagConstraints();
    c.gridx=0;
    c.gridy=0;
    gridBagLayoutPanel.add(testButton, c);

    frame.setVisible(true);
}

enter image description here

AlpacaMan
  • 465
  • 2
  • 7
  • 24
2

if you want the grid to go all the way to the top, simply set all your weighty = 0 until the last item, set it to any number greater than 0, it will effectively push the rest of the buttons to the top.

Don't forget to also increment your button's gridy value.

Otherwise it will be centered. (the same can be done using gridx and weightx value if you arent using the c.fill = GridBagConstraints.HORIZONTAL property.)

Jeremie D
  • 4,145
  • 1
  • 35
  • 41