• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Layout Manager-mock exam

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is taken from a mock exam:
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(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}

Attemping to compile and run the above code

#Will cause a compilation error - a Layout cannot be set after a component has been added with a preset Layout Manager.
#Will cause a Runtime Exception - a Layout cannot be set after a component has been added with a preset Layout
Manager.
#Will compile cleanly and throw no runtime Exception. Only the button with label "Center" is visible and occupies the whole screen.
#Will compile cleanly an throw no runtime Exception. All the buttons are arranged in a single line. Any other component added in future will follow the rules of the BorderLayout Manager.
#Will compile and run cleanly, but no component is visible.
#Will compile cleanly and throw no runtime Exception. The buttons are arranged as listed below
Button Label Position
CenterCenter
Noerth North
SouthSouth
EastEast
WestWest
Could someone give the explanations for the correct answer provided(#Will compile and run cleanly, but no component is visible).
thanks in advance
Cristi
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi!
You see it is necessary that the layout manager be associated with the container before any component is added. If we associate the layout manager after the components are added and the container is already made visible, the components appear as if they have been added by the previous layout manager (if none was associated before, then the default). Only subsequent operations (such as resizing) on the container use the new layout manager. But if the container was not made visible before the new layout is added, the components are re-laid out by the new layout manager.
In this case since the componenst have been already laid down and layout has been given.Again implementing layout on the components wd lead to the present scenario..
I wd also suggest u to look at this site and have a look at the study notes of velmurugan(look at chaper 11 on layout Managers)

http://www.geocities.com/velmurugan_p/

Originally posted by Cristi Tudose:
this is taken from a mock exam:
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(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}

Attemping to compile and run the above code

#Will cause a compilation error - a Layout cannot be set after a component has been added with a preset Layout Manager.
#Will cause a Runtime Exception - a Layout cannot be set after a component has been added with a preset Layout
Manager.
#Will compile cleanly and throw no runtime Exception. Only the button with label "Center" is visible and occupies the whole screen.
#Will compile cleanly an throw no runtime Exception. All the buttons are arranged in a single line. Any other component added in future will follow the rules of the BorderLayout Manager.
#Will compile and run cleanly, but no component is visible.
#Will compile cleanly and throw no runtime Exception. The buttons are arranged as listed below
Button Label Position
CenterCenter
Noerth North
SouthSouth
EastEast
WestWest
Could someone give the explanations for the correct answer provided(#Will compile and run cleanly, but no component is visible).
thanks in advance
Cristi


 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Cristi,
I am a new entrant and this is my first attempt at replying to anything on this forum. As far as your question is concerned, well the trick i think lies in the setVisible part.The default visibility for all components is true except for Window , Frame and Dialog classes.They must be set true explicitly for these classes at the time of creation of the instance. Since you are not doing it at that time(it is being done after the validate method is called) there is no object(frame) to be seen and then even if you try to change the layout or anything else would not work.
I hope I am right.
Thanx
prashant

Originally posted by Cristi Tudose:
this is taken from a mock exam:
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(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}

Attemping to compile and run the above code

#Will cause a compilation error - a Layout cannot be set after a component has been added with a preset Layout Manager.
#Will cause a Runtime Exception - a Layout cannot be set after a component has been added with a preset Layout
Manager.
#Will compile cleanly and throw no runtime Exception. Only the button with label "Center" is visible and occupies the whole screen.
#Will compile cleanly an throw no runtime Exception. All the buttons are arranged in a single line. Any other component added in future will follow the rules of the BorderLayout Manager.
#Will compile and run cleanly, but no component is visible.
#Will compile cleanly and throw no runtime Exception. The buttons are arranged as listed below
Button Label Position
CenterCenter
Noerth North
SouthSouth
EastEast
WestWest
Could someone give the explanations for the correct answer provided(#Will compile and run cleanly, but no component is visible).
thanks in advance
Cristi


 
Cristi Tudose
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Atin&Prashant
I admited i'm not study complet this part(AWT) but... i'm a little confused about your arguments.
If you only switch the lines which set the LayoutManager could u tell me what's output.You may compile&run,but please give
me a reson
And if you know( or anybody else) some URLs where the AWT is properly treat give me a sign.
Rgds,Cristi

Originally posted by atin sehgal:

Hi!
You see it is necessary that the layout manager be associated with the container before any component is added. If we associate the layout manager after the components are added and the container is already made visible, the components appear as if they have been added by the previous layout manager (if none was associated before, then the default). Only subsequent operations (such as resizing) on the container use the new layout manager. But if the container was not made visible before the new layout is added, the components are re-laid out by the new layout manager.
In this case since the componenst have been already laid down and layout has been given.Again implementing layout on the components wd lead to the present scenario..
I wd also suggest u to look at this site and have a look at the study notes of velmurugan(look at chaper 11 on layout Managers)

http://www.geocities.com/velmurugan_p/


 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cristi,
Read this earlier post http://www.javaranch.com/ubb/Forum24/HTML/001736.html for further explanation.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Prashant Pundrik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cristi,
I tried and was right.If you put the two lines setSize and setVisible right where the constructor is being define i.e after the line add(bCenter) you will get a frame showing five buttons arranged in a row as per the layout manager which is set to Flow.
in the first line of the constructor.
Regards
Prashant

Originally posted by Cristi Tudose:
Atin&Prashant
I admited i'm not study complet this part(AWT) but... i'm a little confused about your arguments.
If you only switch the lines which set the LayoutManager could u tell me what's output.You may compile&run,but please give
me a reson
And if you know( or anybody else) some URLs where the AWT is properly treat give me a sign.
Rgds,Cristi


 
Cristi Tudose
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I repet the important code:
�.
Button bNorth = new Button("North")

Button bCenter = new Button("Center");
setLayout(new FlowLayout());//0
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
//set a new layout manager
setLayout(new BorderLayout());//1
validate();
setSize(300,300);
setVisible(true);
any combinations of layouts(FlowLayout,BorderLayout,GridLayout) from line 0 to line 1 work fine(the components are re-laid out by the new layout manager, re-set in line 1),EXCEPT we set a BorderLayout-as we do in line 1.
as I read in a earlier post(thanks to Jane Griscti) the clue is: both FlowLayout and GridLayout implement the LayoutManager interface,while the BorderLayout implements the LayoutManager2 interface-that part is true.
And further the suposition:
BorderLayout calls the method 'invalidateLayout(Container target)' (from LayoutManager2 interface) when it is set to any container.
This method invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
Could anyone know if it's true???
I checked out all my books and I didn't find the answer.
Please a solution...
Rgds,
Cristi
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cristi,
I took a look at the API for both LayoutManager2 and BorderLayout. BorderLayout does implement LayoutManager2 and LayoutManager2 does have an invalidateLayout() method but there was no indication of it being called; so I took a look at the source code for BorderLayout.
Neither constructor calls invalidateLayout().

The API does say that LayoutManager2 is used by classes that know how to layout containers based on Constraints and the addLayoutComponent() method requires a Constraints object.
LayoutManager.addLayoutComponent() does not require a Constraints object.
So, what I think happens is when you add components using a Layout that implements LayoutManager, no Constraints exist. When you then switch to a Layout which implements LayoutManager2, as the existing components have no associated constraints, the default constructor sets everything to 0,0 ... which means none of the components are visible. Calling validate() doesn't help as BorderLayout doesn't know where to put the existing components.
Not sure if I've got this 100% right ... if anyone knows better please jump in.
Hope that helps.
Jane


[This message has been edited by Jane Griscti (edited January 07, 2001).]
 
Cristi Tudose
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jane,
Thanks for trying to give an plausible explanation.
But,I'm a little confused about addLayoutComponent()
What is the connection with add() and setLayout() ?Could you be more exactly?
All I found in API about addLayoutComponent() in BorderLayout is:
"Adds the specified component to the layout, using the specified constraint object. For border layouts, the constraint must be one of the following constants: NORTH, SOUTH, EAST, WEST, or CENTER.
Most applications do not call this method directly. This method is called when a component is added to a container using the Container.add method with the same argument types."
Best rgds Jane,
Cristi
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cristi,
Well I was poking around in another book last night (Java Algorithms by Scott Robert Ladd) I think I found the right answer
Basically, Containers keep track of the Components they contain but it is the LayoutManager that actually handles the positioning of the Components. Each Container can be associated with only one LayoutManager or have a null LayoutManager which means you have to supply the code to handle positioning and resizing separately.
LayoutManagers contain a number of methods: addLayoutComponent(), layoutContainer(), etc. However, in most situations you don't call these directly. Instead, you use the Container methods which in turn call the LayoutManager methods ie Container.add() calls LayoutManager addLayoutComponent().
The key to the difference between LayoutManagers which require constraints such as BorderLayout, and those which don't have constraints such as FlowLayout is this:

Classes implementing LayoutManager2 ie BorderLayout, CardLayout and GridBagLayout maintain an internal list of their components.
Classes implementing LayoutManager ie FlowLayout, GridLayout query the target container for a list of Components

This is why we see the behaviour shown in the original example. When components are added to a Container, the Container itself keeps a list of the objects. If, for example, the Layout has been set to FlowLayout, and the Container is resized, the FlowLayout manager will get a list of the component objects associated with the Container and then perform the size and position functions.
If the Layout is then changed to, for example, BorderLayout, and the Container is resized, no request is made to the Container for a list of the associated Components. The BorderLayout contract states it will keep it's own list of Components; as far as it's concerned, it has no components and nothing needs to be positioned.
This is why the Components need to be re-added to the Container.
Hope thats clearer.
Jane

------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Cristi Tudose
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jane,
Your explanations lighten me.Now I'm sure this is the correct answer.
Please,could u tell me if in SCJP exam ,might apper questions and invloved answers related to deprecated method from Thread class ??(ie,stop(),suspend()/resume())
best rgds,
cristi
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cristi,
Apologies for the late response (ISP problems the last few days).
I don't remember encountering any questions on deprecated methods.
Ajith, Bill, Angela, Marcus ... have you seen anything related to deprecated methods on the exam?
Jane
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. Most likely questions on deprecated feature(s) will not be included in the exam.
Ajith
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow!. Thats a beauty..
Thanks a lot Jane!!. For clearing this long-time doubt for me about changing layout managers. I am now reading all the threads in this forum and i believe this is worth than buying 4 or 5 certification books. I am bringing this thread again (i think this is a very catchy concept) to other students who haven't visited this forum before today.
 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jane!
wonderful explanation
really cool
thanks for sharing such things
------------------
KS
 
Always look on the bright side of life. At least this ad is really tiny:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic