Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AWT Container

 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which cannot be added to a Container?
A. a Menu
B. a Panel
C. an Applet
D. a Component
E. a Container
Answer: A
I read somewhere else that a Menu CAN be added to a container. What goes?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Menu can be added to MenuBar or a MenuItem but as Menu do not decend from Component it can't be added to any container.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you read that ?
Ask yourself of what type a Menu is (see its hierarchy) and what types can be added to a Container...
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Menu is a MenuComponent which is NOT the same thing as a Component.
Containers hold Components - NOT MenuComponents.
MenuContainers hold MenuComponents.
Frame can "setMenuBar" and the MenuBar can hold a Menu.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, rewrite my sentences:
Menu is a MenuItem and it do not descend from Component.
Menu cannot directly be added to a Container because do not descend from Component.
But, Menu can be added to a MenuBar or another Menu. And a MenuBar can added to a Container a Frame.
And PopupMenu can be added to any Component.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carlos,
But, Menu can be added to a MenuBar or another Menu. And a MenuBar can added to a Container a Frame.

MenuBar cannot be added to a Container, but only to a Frame !!!
(I hope it was a typo though )
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>Containers hold Components - NOT MenuComponents.
>MenuContainers hold MenuComponents.
what are MenuContainers ?

Originally posted by Cindy Glass:
A Menu is a MenuComponent which is NOT the same thing as a Component.
Containers hold Components - NOT MenuComponents.
MenuContainers hold MenuComponents.
Frame can "setMenuBar" and the MenuBar can hold a Menu.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What are MenuContainer?

MenuContainer is an interface implemented by the following classes Component, Menu, MenuBar and Frame.
See theMenuContainer Interface in the API
And to answer your private message,

But Frame is a Container though

that's true but a Container is not a Frame and that's why you can't add a MenuBar to a Container.
HIH
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark,
please post your question here instead of sending private messages so that everybody can benefit

i had meant that we know that classes which inherit under the heirarchy
of MenuComponents like Menu, MenuItem etc cannot be added to Components
as their heirarchy is different.
fine, but then suddenly there are exceptions like MenuBar can be added to
Frame, or PopMemu can be added to any Component. so can you tell me what
interface or which method they have or implement or what is it that makes
them apart.
i hope u got my point. I have drawn two heirarchy digrams one for
Component and the other for MenuComponent and the special conditions for
MenuBar and PopupMenu is bothering me. In the sense that i want to why ?

You are right there are two distinct hierarchies, one for Components and one for MenuComponents.
A MenuBar can only be added to a Frame because only that class has a setMenuBar method taking a MenuBar as argument.
PopupMenu can be added to any Component.
From Java AWT: Popup Menu

...
Popup menus can be attached-to/removed-from any component, using the methods in class java.awt.Component:
-add(PopupMenu popup)
-remove(MenuComponent popup)
Note that a popup menu can only be owned by one component at a time.
...

I hope that helps clear your doubt...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic