Author Topic:   All components inherit menucontainer???
Nalini Mistry
ranch hand
posted May 06, 2000 05:23 AM             
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
bartender
posted May 08, 2000 07:42 AM             
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
------------------------------------------------


public interface MenuContainer {
Font getFont();
void remove(MenuComponent comp);
boolean postEvent(Event evt);
}


The source code for Component
------------------------------------------------


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;
}
}
}
}


The source code for Frame
------------------------------------------------

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);
}
}
}

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

Nalini Mistry
ranch hand
posted May 09, 2000 02:54 AM             
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!!!

|