• 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

Abhilash's site AWT

 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question from Abhilash's site with some experimenting of my own.Consider the follwing 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 FlowLayout());
add(bCenter);
add(BorderLayout.SOUTH,bSouth);
add(bWest,BorderLayout.WEST);
add(bEast,BorderLayout.EAST);
add(bNorth,BorderLayout.NORTH);

setLayout(new BorderLayout());


//setLayout(null);
//validate();
//pack();
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
tf.setLayout(new FlowLayout()); //after resizing the frame takes flowlayout
}
}

If u run this code there is only a frame constructed,no buttons are visible.How come,coz I think that the center button should be visible since it is using the add() which is valid for FlowLayout.I agree that the other buttons shouldn't be seen.
If you interchange the setLayout() viz,first BorderLayout then FlowLayout then the Frame is set to FlowLayout,why may I ask??
Help me somebody,
Desperately seeking help,
Vedhas.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vedhas,
When you extend the Frame class the default layout is BorderLayout. In your code the following is occuring:
1. an instance of testFrame is created - at this time the default layout is BorderLayout
2. the object reference(tf) then has its default layout changed
from BorderLayout to FlowLayout
3. then the constructor is called which again sets the objects layout to FlowLayout(which no longer has control)
4. buttons are added to the frame, the 'add' method ignores the parameter order and border layout alignment properties.
5. the frame layout is then changed to borderlayout which again no longer has control
--by adding the line "tf.setVisible(true);" after creating the object you should see the buttons on the frame. To summerize, I belive once you've created an instance the layout must be modified on that object reference.
-PP
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code simply tells the way in which the flow layout & border layout behaves; rather how the classes implement layoutManager or LayoutManager2 interfaces.
Flowlayout will query the container regarding the previuos components added and accomodate them along with the further added components.
Borderlayout,Cardlayout, Gridbaglayout wont do this. They wont query the container. Only the components that are added further to the new layout can be made visible.
HTH
tvs

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic