views:

260

answers:

2

With the following code, I want to create a Jpanel generic component and add two sub components, a Label and a JTextField. I am to add the components, but they do not left align.

(No, I don't need GridBagLayout, but I was trying to do a basic example with GridBag. Can you describe how to do this with GridBag and not another layout).

public Component buildStatusComponent() {

    // Place the components on one line //
    final GridBagLayout layout = new GridBagLayout(); 
    final GridBagConstraints constraints = new GridBagConstraints();              
    final JPanel group = new JPanel(layout);

    constraints.anchor = GridBagConstraints.WEST;
    constraints.gridx = 0;        
    constraints.fill = GridBagConstraints.NONE;
    group.setAlignmentX(JComponent.LEFT_ALIGNMENT);
    group.add(new JLabel("Messages: "), constraints);

    constraints.gridx = 1;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    group.add(this.statusArea, constraints);        

    return group;
}

Above this level, I am just adding the JPanel and filling horizontally.

constraints.gridy++;
constraints.fill = GridBagConstraints.HORIZONTAL;
this.add(this.buildStatusComponent(), constraints);
+1  A: 

you don't seem to fully be using the GridBagConstraints. Instead of typing group.setAlignmentX(JComponent.LEFT_ALIGNMENT) (which I never seem to get to work for anything haha) just use the contraints.anchor = GridBagConstraints.WEST.

A lot of people do not like GridBagLayout but I find it easy to work with once you understand how it works. The most difficult thing to deal with is weights of object in the grid.

But specifically with this question you just want to anchor a component to a side with the GridBagConstraint constants and rely on the constraints class to control where things go.

EDIT: ok, then give them weight. (although this may not solve the problem hehe)

constraints.weightx = 1d;

What is happening is that everything has the same weight so it is spacing out evenly I believe. If you want to 'cram' the items over you can add an empty JPanel after the last component and set fill = REMAINDER and have that be weight 1 and the other components be weight 0. Weight sort of determines how much one component can 'shove' around another.

GridBagConstraints gbc = new GridBagConstraints();
JPanel p = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;

p.add(new JLabel("A"), gbc);
gbc.gridx++;
p.add(new JLabel("B"), gbc);
gbc.gridx++;
gbc.weightx = 1d;
gbc.fill = GridBagConstraints.REMAINDER;
p.add(new JPanel(), gbc);
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0d;
.... continue on filling stuff...

This is one way of dealing with it. Playing around with it a bit until you get a feel for how it works is good.

Arthur Thomas
I tried that, it did not move the components from the center.
Berlin Brown
A: 

The problem with posting a few random lines of code is that we don't know the context of how the code is used.

The section from the Swing tutorial on How to Use GridBagLayout explains why using a weightx constraint is important. Since both your parent and child panels are using a GridBagLayout, we can't tell where the problem is.

If you need more help post your SSCCE that demonstrates the problem.

camickr