views:

265

answers:

2

I am using a GridBagLayout to (currently) display two rows. I am aware this layout is overkill for this task, but am trying to learn how to use it. The problem is that I have added the two panels to the two separate rows and there is a huge gap around the content (see image and code below): alt text

Image background;
    public Table(){
        super();
        ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTableV2.png"));
        background = ii.getImage();
        setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);

        setLayout(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.fill = GridBagConstraints.HORIZONTAL;

        JButton button = new JButton("hello world");
        JPanel panel1 = new JPanel();
        panel1.setPreferredSize(new Dimension(800,100));
        panel1.add(button, BorderLayout.CENTER);
        panel1.setBackground(Color.yellow);
        add(panel1, constraints);

        constraints.gridx = 0;
        constraints.gridy = 1;

        JPanel middlePanel = new JPanel();
        middlePanel.setPreferredSize(new Dimension(800,350));
        middlePanel.add(button, BorderLayout.CENTER);
        middlePanel.setBackground(Color.blue);
        add(middlePanel, constraints);


    }
+2  A: 

Use

constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1d;
constraints.weighty = 1d;

JavaDoc for weightx/weighty says:

Specifies how to distribute extra horizontal/vertical space.

JavaDoc for fill:

This field is used when the component's display area is larger than the component's requested size. It determines whether to resize the component, and if so, how.

Peter Lang
You also cannot reuse `constraints` like that. You need to create a new instance for the second panel.
Devon_C_Miller
+1 This is the right answer...
willcodejavaforfood
+1  A: 

Unfortunately, with GridBagLayout, if the contents do not fill the entire container that it is in, it will automatically center all its contents within its container. That is why you are getting a really large gap.

There are essentially two ways to fix this:

  1. The hard way: Fiddle with the GridBagConstraints. I had limited success with this when trying to avoid the centre-ing behaviour.
  2. The easy way: Put the GridBagLayout inside of a FlowLayout, and then set the alignment of the FlowLayout to top-left, or whatever you wish.

I have asked, and answered, this question myself sometime last week.

So in your case you are adding your panel1 and middlePanel directly to the JFrame (?), with a GridBagLayout

JFrame (GridBagLayout)
 - panel1
 - middlePanel

I would suggest this alternative structure instead, to get rid of all the extra space (and centre alignment as well, if you like).

JFrame (FlowLayout)
 - JPanel (GridBagLayout)
   - panel1
   - middlePanel

HTH

bguiz
-1 This answer is incorrect. What you need to do is to set the weightx and weighty properties on your GridBagConstraints. The default for both will be 0.0 which causes everything to 'center'
willcodejavaforfood
@willcodejavaforfood see my comment on the main question
bguiz