Java GUI - GridBagLayout

Anyone here familiar with the GridBadLayout in Java? I'm trying to make a GUI and I think that GridBagLayout would be the best fit. Here's a picture of what I have in mind:



I've been reading about GridBagLayout on Oracle, but I'm confused by a few things. The way I was taught to do a GUI was as follows:

Code:
//example.java

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

public class example extends JFrame implements ActionListener
{
    Container c;
    JButton button;

    public example()
    {
        super("title goes here");

        c = getContentPane();
        c.setLayout(new FlowLayout());

        button = new JButton("push me");
        button.addActionListener(this);
        c.add(button);

        setSize(500, 500);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        //do whatever here
    }

    public static void main (String args[])
    {
        example application = new example();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Looking at the article, the if(shouldFill) and if(shouldWeightX) conditional statements confuse me. Do I need anything like that for what I want to do?

How would I add the first two rows from my drawing to a GridBagLayout GUI? Assuming I have c as my Container, label as my JLabel, field as my JTextField, and dropdown as my JComboBox, would it be something like:

Code:
c.setLayout(new GridBagLayout());
GridBagConstraints layout = new GridBagConstraints();

layout.gridx = 0;
layout.gridy = 0;    //place it at position (0, 0)
layout.add(label, c);

layout.gridx = 0;
layout.gridy = 1;    //place it at position (0, 1)
layout.add(field, c);

layout.gridx = 1;
layout.gridy = 1;    //place it at position (1, 1)
layout.add(dropdown, c);
That code would go in the constructor. If possible, I'd like to do this as close to the way I learned as possible to avoid confusion. Any advice?

Thanks!
 

FlareBlitz

Relaxed nature. Loves to eat.
is a Tiering Contributor Alumnusis a Top Contributor Alumnusis a Past SPL Champion
The if(shouldWeightX) conditional is used to determine how the program reacts when the user re-sizes the window. You may or may not need it depending on how much you care about GUI adjustment gracefulness. If you skip the conditional, your textboxes might look strange upon re-size, but they would still work as they should.

The if(shouldFill) conditional also has to do with re-sizing behavior. It refers to how the GUI objects expand on contract based on window size changes.

Basically the only differences I can see between the code on Oracle and the way you have taught to do it is that your GUIs don't modify any default values, whereas the example code calls several methods intended to customize the final GUI through a method in GridBagLayout. That's the entire point behind GridBagLayout - it lets your GUI handle user interaction much more gracefully.

I'm not a Java guy (I mostly work with Asp.net and sometimes C#) but hopefully that helped :)
 
Thanks for the help! I decided to do away with the JLabel, the JTextFields, and one of the JButtons (at least for now). I have it working almost exactly the way I want it to. Here's how it currently looks: http://img841.imageshack.us/img841/6717/unledee.jpg

As you can see, it's horizontally-oriented. I want the same thing, only vertically-oriented. I imagine that my problem must lie in the coding of my constraints. Here's that portion of the code:

Code:
GridBagConstraints constraints = new GridBagConstraints(GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, 11, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, insets, 0, 0);
I tried switching the position of the "11" and the "1", thinking that perhaps I misunderstood which was the number of columns and which was the number of rows, but it didn't change the appearance of my GUI at all. More information on this call to the GridBagConstraints constructor can be found here (scroll down to "Constructor Summary").

Any idea what I need to change?
 

Users Who Are Viewing This Thread (Users: 1, Guests: 0)

Top