aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes BorderLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "BorderLayout" Watch "BorderLayout" New topic
Author

BorderLayout

Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
Can somebody why?
import java.awt.*;
public class TestFrame extends Frame
{
public TestFrame()
{
setLayout(new GridLayout());
for(int i = 1 ; i <= 4 ;++i)
{
add(new Button(Integer.toString(i)));
}
pack();
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
1.The code will not compile - the grid layout does not have a no-argument constructor..
2.The code compiles and when run all the buttons are seen in a single column.
3.The code compiles and when run all the buttons are seen in a singe row.
4.The code compiles and when run all button are added one on top or another and only the last one added is
visible.
5.The code compiles , but throws a runtime Exception when components are added.
Why is the answer is 3 and 4, not 3 and 5?
Many Thanks


green horn
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
1. Why did you use a title of BorderLayout when your question was about GridLayout? Attention to detail is required to pass the exam.
2. Looks to me like 3 is the only correct answer. The constructor with no parameters makes a layout of a single row.
Bill
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
Sorry, Bill, Here is the question:
import java.awt.*;
public class TestFrame extends Frame {
Button firstOne = new Button("One");
Button secondOne = new Button("Two");
public TestFrame() {
add(firstOne,BorderLayout.NORTH);
add(secondOne,BorderLayout.NORTH);
setSize(400,400);
setVisible(true);
}
public static void main(String args[]) {
TestFrame tf = new TestFrame();
}
}
An Attempt to compile and run the above piece of code

1.Causes compilation error - a component cannot be added to region which is already occupied by another
component.
2.Causes Runtime Exception - a component cannot be added to region which is already occupied by another
component.
3.Neither i or ii. The Frame comes up and only the button with label "Two" occupies the entire North region of
the Frame.
4.Addition of secondOne causes firstOne to be removed from the container.
5.Addition of the secondOne causes the firstOne to be hidden in the container.
The answer given are 3, 4. But i think should be 3, 5. When i add one line setLayout(new FlowLayout()) before setSize(400,400). The running result showes tow buttons. So the firstOne is not removed from the container. Does anybody agree with me?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I know this might be a radical concept, but did you try compiling and running it to see what happens?
Give Mr. Brogden a cigar... 4 buttons in one row is the correct answer.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Jason, in your second example (the BorderLayout) try adding the line "remove(secondOne);" immediately after the line where you added it. The results will answer your question.
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
Hi, Scott,
i have run it and saw the result. But why the firstOne is still in the container when i set the layout to flowlayout. Can someone explain the trick in the bordlayout? :confused
------------------
green horn
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jason,
No I don't agree with you. The BorderLayout only supports a single component in each area. Therefore, adding another component in the same area that another component already exists causes the first component to be removed.
The FlowLayout has no such restrictions and will place as many components are you have added. The constraints sent into the add function are ignored by FlowLayout.
Regards,
Manfred.
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
Hi, Manfred,
i mean if first setting the layout as BorderLayout, adding the buttons in one area, then resetting the layout to flowlayout, all buttons are there. But as what Scott said, immediately removing the top button without resetting the layout, you can't see the first button added. Does it mean each area of borderlayout-managed container maintain one pointer to a component, but the container maintains a list of components added. Anybody throws some light on it. Thanks
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
If you actually look at how it is implemented you will see that the regions are maintained with 5 fields:
Component north;
Component west;
Component east;
Component south;
Component center;

When you add a component, the positioning constraint is checked with logic like this:


When you remove a component this is the logic used:


However it is the CONTAINER not the LayoutManger that tracks the list of things added. So Container has a list which is a array named components[] in it that is added to with this code:

Then the component is sent to the LayoutManager to be dealt with.
Removing something from a POSITION (the LayoutManagers list) does not remove it from the CONTAINER.
(Edited by Cindy to change code quoted incorrectly from Frame to the Container code actually used)

[This message has been edited by Cindy Glass (edited July 09, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
Thank you, Cindy. i am clear what's going on inside. But for the problem(second post), what is the answer?
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Jason, it appears to me that Cindy's post validates your position -- the answers should be 3 and 5. It appears that adding the second component to the same position as a previous component in a container managed by a BorderLayout causes the second to hide the first. However, removing the second does not cause the first to reappear (I'm not sure how you would do that without simply re-adding the first component). But clearly it's not removed from the container itself, so 4 does not appear to be correct.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
There are in fact, two lists of Components. One kept by the BorderLayout and one kept by the Container. Certain layout managers can be attached to a Container AFTER the components have been added because they use the Container's list - FlowLayout is one of these, but BorderLayout is not.
You have the complete source code for all of these classes with the SDK - learn to "use the source"!
Bill
jayu jadhav
Greenhorn

Joined: Apr 13, 2001
Posts: 13
Container keeps track of all the compo.s. added.
Borderlayout keeps it's own internal list of container's components & DOES NOT ask the container for the list of compo.s
Also it's true with the borderlayout that only the last compo. added to a region is shown.
Whereas Flowlayout asks the container for list of all compo.s.
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
Hi, Thomas,
Be careful on terminology, a layout manager will never be a container.
See the inheritence relationship in API
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Actually I took Bills advice and read some more source (always a good idea) and realized I had incorrectly shown the Frame vector where it should have been the Containers array.
But he is correct, the component is added to the List in the Container, then sent to the LayoutManger for processing. In the case of BorderLayout it is then placed in one of the fields that make up the "list" for that LayoutManager.
Replacing BorderLayout with FlowLayout just causes Flow to look at the Containers list instead of the set of fields in BorderLayout.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BorderLayout