• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Components vs Container (and Menu)

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between a Container and a Component. I know that Component is the Parent class, but whenever you read about Containers, they talk about adding components to it. So I guess my question is this:
Components can contain Containers? Right or wrong.
Container can contain other Containers? Right or wrong.
Container can contian Components? Right or wrong.
Components can contain other Components? Right or wrong.
Menu can be added to a Container? Right or wrong.
Menu can be added to Component? Right or wrong.
HELP ME?!?!?!?
okay that's six questions but I was on a roll...
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between a Container and a Component. I know that Component is the Parent class, but whenever you read about Containers, they talk about adding components to it. So I guess my question is this:
Components can contain Containers? Right or wrong.

Wrong. Containers contain. Components, ah, compone.
The class hierarchy starts out at Component and then breaks into two specialization branches, roughly, "widgets" and "containers". Widgets (Buttons, Labels, TextAreas) can be put into Containers and Containers (Panel, Applet) can be put into Containers but you can't put a Window in a Button. Of course, you can't put a Frame in a Frame either but you can put a Panel in it and then a Panel in it and a Panel in it. This is often used instead of mucking with GridBagLayout to present a nice GUI.
Container can contain other Containers? Right or wrong.
Right. A Panel, for example, can contain other Panels. A Frame can contain an Applet which can contain a Panel. The "contain a" behavior is what is generically implemented in java.awt.Container and more fine tuned in the subclasses.
Container can contain Components? Right or wrong.
Right. Applets can contain Buttons.
Components can contain other Components? Right or wrong.
Wrong. Only Containers can "contain" that is their specialization of the generic properties of their superclass, Component. A Button cannot contain a Button.
Menu can be added to a Container? Right or wrong.
Right. A Menu can be added to a Frame which is a type of container although the exact mechanism uses classes with names other than just "Menu". [see note 1]
Menu can be added to Component? Right or wrong.
Wrong. Containers contain...components don't. [see note 1]
HELP ME?!?!?!?
okay that's six questions but I was on a roll...

[Note 1...there is an exception...Components can have PopupMenus associated with them but they don't really "contain" them. They are the ground from which they "popup". (Got out of that one nicely, eh?) Some mock exam questions turn on this distinction, mentioning either pull down or popup menus in which case you would choose anything that a Component. If they just mention pull down menus, then only Frame or "a Container" depending on the context]
To make matters more complicated, the pieces of a pull down menu are in a completely separate class hierarchy than the one that is headed by Component. This can be really tricky because the subclassing looks backwards but makes perfect sense.
Object<-MenuComponent<-MenuItem<-Menu
Object<-MenuComponent<-MenuBar
It makes sense because a Menu like "Favorites" can contain both MenuItems like "Add Page to Favorites" and "Update Subscriptions" as well as sub-Menus like "Organize Favorites" which in turn have their own MenuItems. This is how Java implements "cascading menus". The top level widget that you add to the Frame is a MenuBar. It is to the MenuBar that you add your Menus like "File", "Edit", "View" and "Go" et cetera.
I hope this helps. Look at the API and then try to write some code. If the compiler tells you "no! bad programmer!" then try to reason it through, look at the API again or look at some examples.
PS see the other folder for an example of a Frame that contains an Applet. I think it's currently on fire.
There is no substitute for coding-compiling-and choking.
Best regards,
Steve Butcher
[email protected]
[This message has been edited by sgwbutcher (edited June 28, 2000).]
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steve,
Correct me if I'm wrong. I think only Frames can contain menus (by way of MenuBar) since only Frames have a setMeunuBar() method. As far as I can see, Applets or Panels or other types of Containers do not have an add(MenuBar) or setMenuBar, so they cannot contain a menu.
 
sgwbutcher
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Steve,
Correct me if I'm wrong. I think only Frames can contain menus (by way of MenuBar) since only Frames have a setMeunuBar() method. As far as I can see, Applets or Panels or other types of Containers do not have an add(MenuBar) or setMenuBar, so they cannot contain a menu.

You're right that only the class Frame can have a MenuBar (by way of add(MenuBar)) but some exam/review questions pick at different aspects of the universe of possible "things you are supposed to know" and may use, sloppily, Container in a generic way and not mean the Container class. Having seen some of the review and mock exam questions, they tend to ask whether:
(1) Whether some list of components can have a pull down menu in which case it is always Frame.
(2) Whether some list of components can have a menu in which case it is always Frame and Menu because Menus can contain Menus.
(3) Whether some list of components can have a menu of any kind in which case it is just about anything because Component permits the addition of PopupMenus...now I've never actually tried to add a PopupMenu to a Scrollbar...
There can be ambiguity introduced when it is not clear if they are talking about a concept or a specific class or classes.
The question that started this thread was from a different folder and went (unattributed)


(2). Which can't be added to a Container?
a.Applet
b.Component
c.Container
d.Menu
e.Panel


The answer is Menu. But I think that you could come up with a generic question where "container" and not Container and menu were the correct answer to "to which of these can you add a pull down menu?"
There is some additional confusion because only since JDK 1.2 has Container been non abstract so it used to be okay to talk about container as in "container in general."
Steve
[This message has been edited by sgwbutcher (edited June 28, 2000).]
[This message has been edited by sgwbutcher (edited June 28, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx...your explanation helped quite a bit. Why can't the books explain it that way? =)
I read the other folder as well, I've never seen code where an applet was added to a container, but that's cool.
Thanx again.
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic