GridbagLayout (Top Left to Bottom)

Hi

I can't figure out why the following code is ignored

gbc.anchor = GridBagConstraints.FIRST_LINE_START;

I want the labels as they are currently, but just starting in the left top corner.

The default anchor according to the documentation is CENTER, but it seems to have no effect changing it.

package demo.layoutmanagers;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import javax.swing.JFrame;

import javax.swing.JLabel;

publicclass TopDownGridbagLayout{

/**

* @param args

*/

publicstaticvoid main(String[] args){

JFrame frame =new JFrame();

frame.setSize(500,300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GridBagLayout gbl =new GridBagLayout();

GridBagConstraints gbc =new GridBagConstraints();

frame.getContentPane().setLayout(gbl);

gbc.anchor = GridBagConstraints.FIRST_LINE_START;

gbc.insets =new Insets(0,0,0,10);

gbc.gridx = 0;

gbc.gridy = 0;

frame.getContentPane().add(new JLabel("Label One"),gbc);

gbc.gridx = 1;

gbc.gridy = 0;

frame.getContentPane().add(new JLabel("Label Two-Two"),gbc);

gbc.gridx = 0;

gbc.gridy = 2;

frame.getContentPane().add(new JLabel("Label Three"),gbc);

gbc.gridx = 1;

gbc.gridy = 2;

frame.getContentPane().add(new JLabel("Label Four"),gbc);

frame.setVisible(true);

}

}

Thanks

[2418 byte] By [CarelDuToita] at [2007-11-26 16:09:28]
# 1

you need to set weightx and weighty

from api docs for weightx

Specifies how to distribute extra horizontal space.

The grid bag layout manager calculates the weight of a column to be the

maximum weightx of all the components in a column. If the resulting layout is

smaller horizontally than the area it needs to fill, the extra space is

distributed to each column in proportion to its weight. A column that has a

weight of zero receives no extra space.

If all the weights are zero, all the extra space appears between the grids of

the cell and the left and right edges.

Michael_Dunna at 2007-7-8 22:31:46 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi Michael

Thanks you for your reply. I tried changing the weightx (and weighty). So the first label is set to the top-left corner, the others though is all over the place :-(. I'm beginning to think that I'm not using the correct layout manager. All I want to do is have two colums of labels from top-left corner going down. The right column should be alligned to the longest label in the left column: Like this ....

Label OneLabel Three

Label Two-Two Label Four

Any ideas ?

CarelDuToita at 2007-7-8 22:31:46 > top of Java-index,Desktop,Core GUI APIs...
# 3

this might be one option

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

public void buildGUI()

{

int rows = 10, cols = 2;

JPanel p = new JPanel(new GridLayout(rows,cols,20,5));

for(int x = 0, y = rows*cols; x < y; x++)

{

p.add(new JLabel(""+(int)(Math.random()*Integer.MAX_VALUE)));

}

JFrame f = new JFrame();

f.getContentPane().add(p);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-8 22:31:47 > top of Java-index,Desktop,Core GUI APIs...