File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about Layout Managers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about Layout Managers" Watch "Question about Layout Managers" New topic
Author

Question about Layout Managers

srini bhav
Greenhorn

Joined: Feb 20, 2001
Posts: 12
In the following applet, how many Buttons will be displayed?

1: import java.applet.*;
2: import java.awt.*;
3:
4: public class Q16 extends Applet
5: {
6: Button okButton = new Button("Ok");
7:
8: public void init()
9: {
10: add(okButton);
11: add(okButton);
12: add(okButton);
13: add(okButton);
14:
15: add(new Button("Cancel"));
16: add(new Button("Cancel"));
17: add(new Button("Cancel"));
18: add(new Button("Cancel"));
19:
20: setSize(300,300);
21: }
22: }
A) 1 Button with label "Ok" and 1 Button with label "Cancel" .
B) 1 Button with label "Ok" and 4 Buttons with label "Cancel" .
C) 4 Buttons with label "Ok" and 1 Button with label "Cancel" .
D) 4 Buttons with label "Ok" and 4 Buttons with label "Cancel" .

The answer was given to be B but I thought it would be A.
Can someone please explain ?
Thanks,
Srini
srini bhav
Greenhorn

Joined: Feb 20, 2001
Posts: 12
Sorry I sent the wrong question. Here is the right question.
import java.applet.*;
2: import java.awt.*;
3:
4: public class Q20 extends Applet
5: {
6: Button okButton = new Button("Ok");
7:
8: public void init()
9: {
10: setLayout(new BorderLayout());
11:
12: add("South", okButton);
13: add("North", okButton);
14: add("East", okButton);
15: add("West", okButton);
16: add("Center", okButon);
17:
18: setSize(300,300);
19: }
20: }
The above Applet will display
A) Five Buttons with label "Ok" at Top, Bottom, Right, Left and Center of the Applet.
B) Only one Button with label "Ok" at the Top of the Applet.
C) Only one Button with label "Ok" at the Bottom of the applet.
D) Only one Button with label "Ok" at the Center of the Applet.


The answer for this is given to be B.
Can someone explain ?
Thanks,
Srini
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

The layout manager is smart enough to see the same object added to the container more than once and ignore multiple adds. So you end up with only one OK button. The Cancel button, on the other hand, is instantiated each time it is added. So while they all look the same, they're difference instances, and the layout manager sees them as such.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
ego hu
Ranch Hand

Joined: Mar 20, 2001
Posts: 53
I compiled that code, the result is:
one OK button at the center of the applet.
Your add() method is of old version. If you want to
use the ability of BorderLayout, you must use
the form add(okButton, BorderLayout.SOUTH); and etc.
Otherwise the button will be added by default to the center.
Even if you use this form, okButton will be added
only once. Because the last sentence is center, so the
button will be at center.
srini bhav
Greenhorn

Joined: Feb 20, 2001
Posts: 12
Thanks but does that mean that you cannot add the same button to all the 5 positions (North, south, east, west and center) ?
ego hu
Ranch Hand

Joined: Mar 20, 2001
Posts: 53
YES.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Layout Managers