• 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

Urgent help please on AWT - mixing layout managers

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abilash's mock exam Q39 has me confused. After Q38 I thought I had a decent idea of what was going on. But why doesn't any button appear when you construct a frame with this code:


import java.awt.*;
public class TestFrame extends Frame
{
Button bNorth = new Button("North");
Button bSouth = new Button("South");
Button bEast = new Button("East");
Button bWest = new Button("West");
Button bCenter = new Button("Center");
public TestFrame()
{
setLayout(new BorderLayout());
add(bSouth,BorderLayout.SOUTH);
add(bWest,BorderLayout.WEST);
add(bEast,BorderLayout.EAST);
add(bNorth,BorderLayout.NORTH);
add(bCenter);
setLayout(new FlowLayout());
validate();
pack();
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}


I thought it would display a single button - the last one added - in the center of the frame, occupying the entire frame. But it doesn't. I obviously dont understand how and when doLayout() is called and components are laid out.
It is tough finding information on AWT. Can someone please give me some info/pointers?
[ May 12, 2002: Message edited by: R Arun ]
[ May 12, 2002: Message edited by: R Arun ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only the Border and Card AWT LayoutManageres don't know how to relayout the components added previously to other LayoutManager. Thus in the example you see all the botons.

this is the old old AWT tutorial from SUN
Also in bruceeckel.com you can download the first edition of Thinking in Java.
Richard G Balwin has written a lot here
 
R Arun
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick response Jose.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arun,
All containers (ie Frame) have an internal list of all components added to them. Component on this list may also have a constraint object attached to them, which will be used by some layout managers to lay out the components.
When you add the components with the BorderLayout manager, it will attach a certain constraint to each component (NORTH, SOUTH, EAST...). At "lay out" time, the BorderLayout manager will check those constraints to decide where to place each component.
When you change layout manager, the container retains all components previously added (not quite sure what happens to the constraints though). In the case of FlowLayout, it will lay out all the componets of the container (ignoring the constraints property). You can check that the same applies for GridLayout.
The problem comes when you set a layout manager that uses constraints (such as BorderLayout). If the constraints are not properly set, this layout will produce strange results.

Eduard
 
Lookout! Runaway whale! Hide behind this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic