• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Layout

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class FrameTest extends Frame{
public FrameTest(){
super("FrameTest");
setSize(300,400)
setVisible(true);
Componrnt b=new Button("B");
Component c=new Button("C");
add(c);
add(b);
}
public static void main(String srgs[]){
FrameTest f=new FrameTest();
}
}
Answer given empty window is displayed.
I feel button B should occupy the whole frame
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neha,
The trick is to know how LayoutManager2 interface works. The layout manager, BorderLayout in this case, only lays out the components that it knows about at the time that it is validated. The setVisible method call internally calls validate so that the layout can manage its children. At that time, no children are present so the frame contains nothing.
Adding children later will not change the displayed layout until it is validated again. Since you have not called validate explicitly or setVisible again the new children won't appear.
Try adding a call to validate() at the end of the constructor to see what happens.
Regards,
Manfred.
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Manfred,
I thought the steps were
1. construct a frame
2. set a layout
3 and then add components.
this implies to Layout Manager2.
If i add components first and then set a layout then i will get an empty frame.
Please explain what the rules are and where do the above mentioned rules apply
Neha
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that too.
After code, compile and ran it, it shows an empty window.
Here's why:
1.- The frame is made visible before the buttons are added, so when it is drawn there's no buttons to show.
2.- The buttons are added to the frame, but as the frame has already been drawn thus they are not showed, if you resize the window with the mouse the B button will appear occupying the whole frame this is because when the frame is changes its size
paint method is called and the button that were added is painted too.
as demo here's a modified code:

Originally posted by Neha Sawant:
public class FrameTest extends Frame{
public FrameTest(){
super("FrameTest");
setSize(300,400)
setVisible(true);
Componrnt b=new Button("B");
Component c=new Button("C");
add(c);
add(b);
}
public static void main(String srgs[]){
FrameTest f=new FrameTest();
}
}
Answer given empty window is displayed.
I feel button B should occupy the whole frame



[This message has been edited by Zkr Ryz (edited November 07, 2001).]
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i think you are right.
well for the button to be displayed at first attempt the setVisible should be after adding buttons.
Am i right?
Neha
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic