Question

I am new in Java and it's hard for me to understand the GridBag-Layout. What I just want to do is to resize Button 1 to a width of three buttons. That means Button 1 shoud start from the coordinates x=0,y=0 and end above the end of button 3 (x=0,y=3).

Here's the code:

package footballQuestioner;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class asd  {

    public static void main(String[] args) {
        JFrame frame = new Houdini();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//      frame.setSize(250, 250);
        System.out.println(frame.getWidth());
        frame.setResizable(true); 
        frame.pack();
        frame.setVisible(true);
    }
}


class Houdini extends JFrame {

    private JButton one=new JButton("one");
    private JButton two=new JButton("two");
    private JButton three=new JButton("three");
    private JButton four=new JButton("four");
    private JButton five=new JButton("five");

    private GridBagConstraints gbc=new GridBagConstraints();
    private JPanel panel=new JPanel(new GridBagLayout());

    public Houdini() {



        //      Insets insets=new Insets(30, 0, 30, 0);
//      gbc.insets=insets;

        gbc.gridx=0;
        gbc.gridy=0;


        gbc.gridwidth=1; 
        gbc.gridheight=1;


        gbc.fill=0;

        panel.add(one,gbc);


        gbc.gridx=1;
        gbc.gridy=1;

        gbc.gridwidth=1;
        gbc.gridheight=1;


        gbc.fill=0;

        panel.add(two,gbc);


        gbc.gridx=2;
        gbc.gridy=2;

        gbc.gridwidth=1;
        gbc.gridheight=2;

        gbc.fill=0;

        panel.add(three,gbc);


//      gbc.gridx=3;
//      gbc.gridy=3;
//      
//      gbc.gridwidth=-4;
//      gbc.gridheight=1;
//      
//
//      gbc.fill=GridBagConstraints.HORIZONTAL;
//      
//      panel.add(four,gbc);

        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        add(panel);

    }
}

It should look like this:

enter image description here

Was it helpful?

Solution

You mean something more like...

enter image description here

Which I used...

gbc.gridx = 0;
gbc.gridy = 0;

gbc.gridwidth = 3;
gbc.gridheight = 1;

gbc.fill = 0;

panel.add(one, gbc);

or

enter image description here

Which I used...

gbc.gridx = 0;
gbc.gridy = 0;

gbc.gridwidth = 3;
gbc.gridheight = 1;

gbc.fill = GridBagConstraints.HORIZONTAL;

panel.add(one, gbc);

to achieve?

Updated

The problem is, from the point of view of GridBagLayout, column 0 has no width...this is why it's been compressed...

What you need to do is add some kind of filler component into the column, which will cause it to put space into it...

For example

Updated with a cheat

Depending on what it is you want to achieve, you could also cheat ;)

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestLayout {

    public static void main(String[] args) {
        JFrame frame = new Houdini();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//      frame.setSize(250, 250);
        System.out.println(frame.getWidth());
        frame.setResizable(true);
        frame.pack();
        frame.setVisible(true);
    }

    public static class Houdini extends JFrame {

        private JButton one = new JButton("one");
        private JButton two = new JButton("two");
        private JButton three = new JButton("three");
        private JButton four = new JButton("four");
        private JButton five = new JButton("five");

        private GridBagConstraints gbc = new GridBagConstraints();
        private JPanel panel = new JPanel(new BorderLayout());

        public Houdini() {

            JPanel top = new JPanel(new BorderLayout());
            top.add(one);
            JPanel center = new JPanel(new GridLayout(0, 3));

            center.add(empty());
            center.add(two);
            center.add(empty());
            center.add(empty());
            center.add(empty());
            center.add(three);

            panel.add(top, BorderLayout.NORTH);
            panel.add(center);

            panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

            add(panel);

        }

        private JComponent empty() {
            JPanel panel = new JPanel();
            return panel;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top