wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Components vs Container (and Menu) 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 "Components vs Container (and Menu)" Watch "Components vs Container (and Menu)" New topic
Author

Components vs Container (and Menu)

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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...
sgwbutcher
Ranch Hand

Joined: May 13, 2000
Posts: 56
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
exceptionraised@aol.com
[This message has been edited by sgwbutcher (edited June 28, 2000).]


-------------------------<BR>Steve<BR>sgwbutcher@aol.com<BR>http://hometown.aol.com/sgwbutcher/
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
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

Joined: May 13, 2000
Posts: 56

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

Joined: Nov 22, 2008
Posts: 18944
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Components vs Container (and Menu)