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).]