rubixibuc
rubixibuc

Reputation: 7427

Java GridBagLayout layout issues

It seems everything is squishing towards the center, but I can't figure out why. Here is my code. I tried to make it easy to read as possible.

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;

/**
 *
 * @author rubixibuc
 */
public class RulesWindow extends JFrame {

    public GridBagLayout layout = new GridBagLayout();
    public GridBagConstraints constr = new GridBagConstraints();
    public DefaultComboBoxModel modelE = new DefaultComboBoxModel();
    public DefaultComboBoxModel modelA = new DefaultComboBoxModel();
    public DefaultListModel modelL = new DefaultListModel();

    public RulesWindow()
    {
        super("Rules Window");
        /* layout for window
        |_P_|__E__|_T_|__A__|_a_|
        |                   |xxx|
        |                   |xxx|
        |                   |xxx|
        |_________L_________|_M_|
         X = no fill
         P = pattern field
         E = extension combobox
         T = target action
         A = action combobox
         a = add rule
         M = remove rule
         L = rule list
        */

        setLayout(layout);

        JTextField P = new JTextField(); // 0,0,1,1 
        JComboBox E = new JComboBox(modelE); // 1,0,2,1
        JTextField T = new JTextField(); // 3,0,1,1
        JComboBox A = new JComboBox(modelA); // 4,0,2,1
        JButton a = new JButton("+"); // 6,0,1,1
        JList L = new JList(modelL); // 1,1,6,4
        JButton M = new JButton("-"); // 6,4,1,1

        changeConstraints(0,0,1,1);
        add(P);
        changeConstraints(1,0,2,1);
        add(E);
        changeConstraints(3,0,1,1);
        add(T);
        changeConstraints(4,0,2,1);
        add(A);
        changeConstraints(6,0,1,1);
        add(a);
        changeConstraints(1,1,6,4);
        add(L);
        changeConstraints(6,4,1,1);
        add(M);

        setSize(200,200);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
    public final void changeConstraints(int gridx, int gridy, int gridwidth, int gridheight)
    {
        constr.anchor = GridBagConstraints.NORTHEAST;
        constr.fill = GridBagConstraints.BOTH;
        constr.gridx = gridx;
        constr.gridy = gridy;
        constr.gridwidth = gridwidth;
        constr.gridheight = gridheight;
    }
    @Override
    public final Component add(Component comp)
    {
        layout.setConstraints(comp, constr);
        super.add(comp);
        return comp;
    }
}

Thanks in advance. I diagrammed what it should look like in the comments.

Also is the way I'm calling the overridden method correct?

Upvotes: 1

Views: 243

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

Whenever I here

It seems everything is squishing towards the center, but I can't figure out why

in association with GridBagLayout, I ask where are the weightx and weighty constraints?

So a possible quick solution: remember to set the GridBagConstraints weightx and weighty fields with reasonable values (you could default this to 1.0 to start out and then play with them).

Upvotes: 2

Related Questions