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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "All components inherit menucontainer???" Watch "All components inherit menucontainer???" New topic
Author

All components inherit menucontainer???

Nalini Mistry
Ranch Hand

Joined: Mar 21, 2000
Posts: 64
A qs in one of the mocks is "What can contain a menu bar?" Options are Frame, Applet, Panel, Window. I would pick all. And it is correct in one test. In another test the same qs has only Frame as the answer and if the others are selected it is incorrect. On going thru the API i found that Component implements the MenuContainer interface. Going by inheritance wouldnt all components inherit this capability of being a Menu Container ?? Does that mean even Buttons/labels/etc that descend from component can contain menubars?? (Doesnt make sense, just speaking rules-of-inheritance wise) Looking for methods, only frames have the setMenuBar method to set a menu. rest use add??
I am a little confused about the correct answer. To be it seems that all concrete classes of components would inherit the capability to be a menucontainer from Component. Can anybody shed some light, (preferably a halogen one) on this topic??
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Nalini,
Here is little (may be 70 watts ) help.
To clear your doubt, I again digged into both Sun's source code and the archived AWT tutorial from Sun.
From Sun's AWT tutorial
Menu functionality in the AWT is provided by several classes. These classes do not inherit from Component, since many platforms place severe limits on menu capabilities. Instead, menu classes inherit from the MenuComponent class.
To be able to contain a MenuComponent, an object must adhere to the MenuContainer interface. The Frame, Menu, and MenuBar classes are the only AWT classes that currently implement MenuContainer.
In order to verify what is said in the Tutorial, I extracted the source code for both 'Component' and 'Frame' classes. I found that eventhough the documentation for 'Component' class in the API and in the source code also says 'implements MenuContainer', internally ONLY the 'Frame' class FULLY /gives TRUE implemetation for the remove(MenuComponent m) method from the MenuContainer interface. The class Component gives partial implementation of the MenuComtainer interface. Partial implementation in the sense, Component class is implemnted to remove ONLY PopupMenu NOT All MenuComponents.
And also as you told, Only Frame has got a method setMenuBar(MenuBar m) to attach a menu. All decendents from Component class have only add(Component c) related methods. Since the java.awt.MenuComponent hirearchy is COMPLETELY a DIFFERENT branch we can't attach a MEnuComponent to a Component. This also adds to our argument of Only Frames can have Menu bars .
So for your qstns we have to select only Frame.
regds
maha anna
The source code for MenuContainer interface
------------------------------------------------
<pre>
public interface MenuContainer {
Font getFont();
void remove(MenuComponent comp);
boolean postEvent(Event evt);
}
</pre>
The source code for Component
------------------------------------------------
<pre>
public synchronized void remove(MenuComponent popup) {
if (popups != null) {
int index = popups.indexOf(popup);
if (index >= 0) {
PopupMenu pmenu = (PopupMenu)popup;
if (pmenu.peer != null) {
pmenu.removeNotify();
}
pmenu.parent = null;
popups.removeElementAt(index);
if (popups.size() == 0) {
popups = null;
}
}
}
}
</pre>
The source code for Frame
------------------------------------------------
<pre>
public void remove(MenuComponent m) {
synchronized (getTreeLock()) {
if (m == menuBar) {
menuBar = null;
FramePeer peer = (FramePeer)this.peer;
if (peer != null) {
mbManagement = true;
if (valid) {
invalidate();
}
peer.setMenuBar(null);
m.removeNotify();
}
m.parent = null;
} else {
super.remove(m);
}
}
}
</pre>

[This message has been edited by maha anna (edited May 08, 2000).]
Nalini Mistry
Ranch Hand

Joined: Mar 21, 2000
Posts: 64
Thanx a lot MA!!! thanx thanx thanx a million!!! I got an qs on exactly this topic. Whatever % one qs is, of the 88% that i scored, i am deeply indebted to you for !!! Its been really great of you to ans every qs that you do so clearly and explicitly. Love you!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: All components inherit menucontainer???