• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Anchor in GridBagLayout

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What could be the effect of Anchor field in GridBagLayout, if the Fill field is set to BOTH ??
In the following program, I am displaying three buttons; the first two at the top left, one below the other and the Third one at the bottom right.

import java.awt.*;
public class AnchorTest extends Frame{
AnchorTest() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
Button b = new Button("1st button");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
//gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = 2;
add(b,gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.BOTH;
Button n = new Button("2nd button");
add(n,gbc);
gbc.gridx = 2;
gbc.gridy = 2;
Button n2 = new Button("3rd button");
add(n2,gbc);
setSize(150,150);
setVisible(true);
}
public static void main(String[] args){
new AnchorTest();
}
}

I don't see any difference in the output even if I include the commented anchor field.
If I set the fill field to NONE (by commenting that line), I am seeing the effect of anchor field.
Can someone explain the purpose of anchor field in detail ??
Thanks

Regards
Balaji

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You are seeing quite the expected behaviour. when you set the fill to BOTH, the element will occupy the whole of the cell. Then there is no effect of the anchor. Whatever the value you give to anchor, the display of the element will be the same.
Shree.
reply
    Bookmark Topic Watch Topic
  • New Topic