GBL question

I've been trying to master the GBL .. hahaha!

in my reading i see that an anchor controls the location of the component within its cell (if it is smaller than its display cell).

and the weight{x,y} control how extra space is distributed to cells.

so why is it that i can't do something like this and have the button show up in the upper left corner?

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.border.LineBorder;

publicclass GridBadLayoutTestextends JPanel{

private GridBagLayout gbl =new GridBagLayout();

private GridBagConstraints c =new GridBagConstraints();

public GridBadLayoutTest(){

super();

init();

}

privatevoid init(){

setBorder(new LineBorder(Color.RED, 2));

setLayout(gbl);

setPreferredSize(new Dimension(500, 500));

JLabel label =new JLabel("Label 1");

label.setBorder(new LineBorder(Color.BLACK, 2));

c.anchor = GridBagConstraints.FIRST_LINE_START;

//c.weightx = 0.01;

//c.weighty = 0.01;

c.fill = GridBagConstraints.NONE;

add(label, c);

}

privatestaticvoid createAndShowGUI(){

//Create and set up the window.

JFrame frame =new JFrame("ActionDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create/set menu bar and content pane.

GridBadLayoutTest demo =new GridBadLayoutTest();

demo.setOpaque(true);

frame.setContentPane(demo);

//Display the window.

frame.pack();

frame.setVisible(true);

}

publicstaticvoid main(String[] args){

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

createAndShowGUI();

}

});

}

}

if you uncomment the two weight{x,y} lines, it looks like i expect it to. what ive found is that if i anchor horizontally i have to set the weightx and if i anchor vertically i have to set weighty. if i anchor something with both horz and vert .. i have to set both. (you must set them to > 0.0 because setting them to 0.0 is the same as not setting them).

so why does the left over distribution of space affect an anchor? the two just dont seem to be related to me. and nor have i found any documentation that says "anchor wont work unless ..."

ive seen it mentioned that fill does no good if weightx and weighty are not set...but nothing about anchor.

[4568 byte] By [txjumpa] at [2007-10-3 10:04:38]
# 1

in my reading i see that an anchor controls the location of the component within its cell (if it is smaller than its display cell).

GridBagLayout class api comments section: anchor paragraph.

The weightx, weighty paragraph explains that components remain clumped together in the dimension whose corresponding weight constraint is zero (the default value), ie, the display area equals the component size.

The anchor constraint has no affect unless a components display area is larger than the components size which means that there is room for the component to move around inside its didplay area. The weight constraints control how extra space, if any, is allocated to the display area of child components.

The GridBagLayout api points this out in the two paragraphs mentioned above but it takes some study, experimenting, and practice to really understand.

crwooda at 2007-7-15 5:23:47 > top of Java-index,Desktop,Core GUI APIs...
# 2

im still kinda confused at how they are related in that anchor wont work unless weight is set. but i will keep reading.

hehehe..was playing while responding... this does what i want it to but i wouldnt have just guessed that it worked like this.

private void init() {

setBorder(new LineBorder(Color.LIGHT_GRAY, 2));

setLayout(gbl);

setPreferredSize(new Dimension(500, 500));

JLabel label = new JLabel("Label 1");

JLabel label2 = new JLabel("Label 2");

label.setPreferredSize(new Dimension(80, 20));

label.setBorder(new LineBorder(Color.BLACK, 2));

label2.setPreferredSize(new Dimension(80, 20));

label2.setBorder(new LineBorder(Color.BLACK, 2));

c.anchor = GridBagConstraints.FIRST_LINE_START;

c.gridwidth = GridBagConstraints.REMAINDER;

c.weightx = 0.0;

c.weighty = 0.0;

add(label, c);

c.weightx = 1.0;

c.weighty = 1.0;

add(label2, c);

}

leave the anchor to something at the top (north, first line start, etc)

set label1 to weight = 0

set label2 to weight = 1

if you dont set label2 weight > 0, they dont anchor. the just sit in the middle. that just doesnt make sense to me but i will just go with it until i understand it!

txjumpa at 2007-7-15 5:23:47 > top of Java-index,Desktop,Core GUI APIs...