GridBagLayout

I'm trying to use the GridBagLayout for a panel i'm designing. The problem is that when I compile and run the app, the JButton gets placed directly in the middle of the panel, instead of at 0,0 like I coded it to. Can someone tell me what's wrong with this code?

import javax.swing.*;

import java.awt.*;

publicclass Test

{

publicstaticvoid main(String[] args)

{

MyFrame x =new MyFrame();

}

}

class MyFrameextends JFrame

{

public MyFrame()

{

setDefaultCloseOperation(EXIT_ON_CLOSE);

setBounds(200,200,200,200);

add(new MyPanel());

setVisible(true);

}

}

class MyPanelextends JPanel

{

public MyPanel()

{

setLayout(new GridBagLayout());

JButton button =new JButton("Click");

add(button, getConstraints(0,0,3,3, GridBagConstraints.EAST));

}

private GridBagConstraints getConstraints(int gridx,

int gridy,int gridWidth,int gridHeight,int anchor)

{

GridBagConstraints c =new GridBagConstraints();

c.insets =new Insets(5, 5, 5, 5);

c.ipadx = 0;

c.ipady = 0;

c.gridx = gridx;

c.gridy = gridy;

c.gridwidth = gridWidth;

c.gridheight = gridHeight;

c.anchor = anchor;

return c;

}//end getConstraints

}

[2673 byte] By [kwiknessa] at [2007-11-27 5:00:59]
# 1

It doesn't create a grid, per se, except in relation to other components. So having only 1 component in the panel doesn't give that component anything else to be relative to.

add(b1, getConstraints(0,0,0,0, GridBagConstraints.NORTH_EAST);

add(b2, getConstraints(1,1,0,0, GridBagConstraints.NORTH_EAST);

bsampieria at 2007-7-12 10:18:10 > top of Java-index,Desktop,Core GUI APIs...
# 2
Oh ok. Makes perfect sense. Thanks a bunch. 5 points to you!
kwiknessa at 2007-7-12 10:18:10 > top of Java-index,Desktop,Core GUI APIs...
# 3

Ok, now I've updated my code, but now, the components are being placed directly on top of one another. Why is this happening? The new code in my panel constructor is:

public MyPanel()

{

setLayout(new GridBagLayout());

JButton shuffle = new JButton("Shuffle");

JButton deal = new JButton("Deal");

add(shuffle, getConstraints(0,0,0,0, GridBagConstraints.WEST));

add(deal, getConstraints(1,0,0,0, GridBagConstraints.WEST));

}

kwiknessa at 2007-7-12 10:18:10 > top of Java-index,Desktop,Core GUI APIs...
# 4
actually, grid width/height should probably be 1.Otherwise, they should be on the same line.
bsampieria at 2007-7-12 10:18:10 > top of Java-index,Desktop,Core GUI APIs...
# 5
Even so, the components aren't placed at the top-left of the panel. How could I accomplish this?
kwiknessa at 2007-7-12 10:18:10 > top of Java-index,Desktop,Core GUI APIs...
# 6

Here, try this code:

setDefaultCloseOperation(EXIT_ON_CLOSE);

//setBounds(200,200,200,200);

//getContentPane().add(new MyPanel());

getContentPane().setLayout(new GridBagLayout());

JButton button = new JButton("Click");

Insets insets = new Insets(0,0,0,0);

GridBagConstraints constraints = new GridBagConstraints(

0, 0, // gridx, gridy

1, 1, // gridwidth, gridheight

0.5, 0.5, // weightx, weighty

GridBagConstraints.NORTHWEST, //CENTER, // anchor

GridBagConstraints.BOTH, // fill

// GridBagConstraints.NONE, // fill

// GridBagConstraints.VERTICAL, // fill

// GridBagConstraints.HORIZONTAL, // fill

insets, // insets

0, // ipadx

0); // ipady

getContentPane().add(button, constraints);

JButton button2 = new JButton("Click");

constraints = new GridBagConstraints(

1, 0, // gridx, gridy

1, 2, // gridwidth, gridheight

0.25, 0.25, // weightx, weighty

GridBagConstraints.NORTHWEST, //CENTER, // anchor

GridBagConstraints.BOTH, // fill

// GridBagConstraints.NONE, // fill

// GridBagConstraints.VERTICAL, // fill

// GridBagConstraints.HORIZONTAL, // fill

insets, // insets

0, // ipadx

0); // ipady

getContentPane().add(button2, constraints);

JButton button3 = new JButton("Click");

constraints = new GridBagConstraints(

0, 1, // gridx, gridy

1, 1, // gridwidth, gridheight

0.25, 0.25, // weightx, weighty

GridBagConstraints.NORTHWEST, //CENTER, // anchor

GridBagConstraints.BOTH, // fill

// GridBagConstraints.NONE, // fill

// GridBagConstraints.VERTICAL, // fill

// GridBagConstraints.HORIZONTAL, // fill

insets, // insets

0, // ipadx

0); // ipady

getContentPane().add(button3, constraints);

pack();

setVisible(true);

Your problem was cause by a combination of things ... using setBounds instead of pack, not using weighty and weightx, not positioning them properly on the grid ... you need to change the constrainsts for each Component.

abillconsla at 2007-7-12 10:18:10 > top of Java-index,Desktop,Core GUI APIs...
# 7

Probably like this, but that assumes there's no specific requirement to use GridBagLayout (which there isn't unless it's some class assignment).

public MyPanel()

{

setLayout(new BorderLayout());

JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));

JButton shuffle = new JButton("Shuffle");

JButton deal = new JButton("Deal");

buttons.add(shuffle);

buttons.add(deal);

add(buttons, BorderLayout.NORTH);

}

bsampieria at 2007-7-12 10:18:10 > top of Java-index,Desktop,Core GUI APIs...
# 8

In your example - try this:

public MyPanel()

{

setLayout(new GridBagLayout());

JButton button = new JButton("Click");

JButton button2 = new JButton("Click Again");

add(button, getConstraints(0,0,1,1, GridBagConstraints.CENTER));

add(button2, getConstraints(1,0,2,1, GridBagConstraints.CENTER));

}

abillconsla at 2007-7-12 10:18:10 > top of Java-index,Desktop,Core GUI APIs...
# 9
>you need to change the constrainsts for each> Component.I have a getConstraints method also in the class.
kwiknessa at 2007-7-12 10:18:10 > top of Java-index,Desktop,Core GUI APIs...
# 10
Yes I see that - and if you change your code as per my post you will see that it works.
abillconsla at 2007-7-12 10:18:10 > top of Java-index,Desktop,Core GUI APIs...