• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

panel vs container

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls. look at the following code

If the statement Panel p = new Panel(); was replaced by Container p = new Container();, no button is visible,why?
what's the most significant difference between Panel and Container?
Thanks in advance
James
[This message has been edited by James Du (edited May 13, 2001).]
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi james,
1.)Container is an abstract subclass of Component.
As a result u can't directly instantiate it.
2.)Panel is a concrete subclass of Container.
Therefore if u write:
Container p=new Panel(); instead of
Container p=new Container;
then the button will be visible.
rajashree.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajashree,
If you check the JDK API u'll find that Container is not an Abstract class.
James I have changed your code and the button shows.
What I couldn't understand was ,the background color is not set.
import java.awt.*;
public class CompLay extends Frame{
public static void main(String argv[]){
CompLay cl = new CompLay();}
CompLay(){
// if replaced by the following comment line, no button is visible, why?
//Panel p = new Panel();
Container p = new Container();
p.setLayout(new FlowLayout());
p.setBackground(Color.pink);
p.add(new Button("One"));
add("South",p);
setSize(300,300);
setVisible(true);
}}
Can some one tell why the color doesnt show pink ??
Rgs
M
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,
Further, to what Rajshree has said if place
<code> Container p = new Panel(); </code>.
instread of <code> Container p = new Container(); </code>
Then the button does shows-up.
Hope this clarifies your querry.
Ravindra Mohan.
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for quick reply
mousami, I had tried what you tried and found the same outcome.can't understand why.I checked the API of constructor of container, doubt whether it's caused by lightweight? hope someone could give a detailed explanation.
Ravindra, I know if a panel is created instead of a container, the button would be visible, but i want to know the reason
Take care
James
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mousomi, i have gone thro' java APIs and found out that Container is not an abstract class.Thanx for correcting me.
i would like to know what is the default layout manager of Container.i mean is it BorderLayout or FlowLayout or some other layout?if FlowLayout, then why do we have to set it by writing
setLayout(new FlowLayout());
does that mean Container doesnot have any layout manager of its own?setLayout() method has to be used always.
Hope some1 answers my query!
rajashree.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In older versions of the JDK, Container was abstract. Now it is not abstract.
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thomas, pls. dont't simply tell it's abstract or not, why the phenomenon?
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rajashree ghatak:

i would like to know what is the default layout manager of [b]Container
.i mean is it BorderLayout or FlowLayout or some other layout?if FlowLayout, then why do we have to set it by writing
setLayout(new FlowLayout());
does that mean Container doesnot have any layout manager of its own?setLayout() method has to be used always.
Hope some1 answers my query!
rajashree.[/B]


Could some1 kindly throw some light on my above query for which i have'nt yet recieved any reply?
rajashree.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default constructor for Panel has exactly ONE line of code. That is to create a new FlowLayout(). That is why that is it is the default for Panel.
The default constructor for Container is empty. Therefore it has NO LayoutManager set until you specifically set one.
Of course the interesting question is why the setBackground() method does NOT set the background for a Container. That took some poking around. Apparently when you create a Panel, the addNotify method is automatically called for you (from the super class Container) and is used to align the Panel with it Peer component in the Native operating system. The Native Peer actually controls the look of the Panel.
When you create a Container it does NOT align itself with any Peer component on the operating system, it just calls the addNotify's for any children that it has. No Peer = No one to do the dirty work of the look, like setting the background color. The Container has the methods that go through the motions, but no Peer to carry the actions out.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question at this topic. The line:
add("South",p);
doesn't that need BoarderLayout to work properly? Is the "South" parameter just ignored du to FlowLayout being chosen instead?
/Mike
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Container has an add method that takes
add(String name, Component comp)
So This component gets added to the Container. Then it will show up in any LayoutManager that knows how to deal with those constraints.
FlowLayout and GridLayout don't take any constraint information in, so they don't look for it. They just deal with the components on a first come first serve basis, placing them from left to right. So in this case no problem.
Any LayoutManagers that DO take constraints are going to look to see if the constraints that were used to add the component to the panel are good enough for them to use. If not, then the component will not get displayed.
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cindy
I dont quite understand the method addNotify and the concept Peer component you mentioned above, just presume they are related to the way the AWT was implemented in a specific operating system. Could you please give a explanation? what design pattens were used here? abstract factroy, facade, or bridge? could i look at Toolkit as a facade? how i consider the awt components and their peers?
Thanks
James
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the AWT in order to get the components and windows and frames etc to work, the classes use JNI calls (using native code) to get the operating system to do work for them. The operating system equivalent of the java AWT thing calling it, it called it's Peer.
The AWT classes inherit addNotify from Container. In the lower lever classes that method is overridden to hook up with the OS peer that relates to it. You don't need to do anything to make this happen.
The Peer components work using "Callbacks".
Now you are going to make me get out my Design Patterns book and actually READ it again
It looks like more of a facade than a bridge, with the AWT components acting as a go-between between you and the raw operating system. This allows YOUR code to be system independant (even though the JRE/JDK classes are system dependant - and you just download the correct version).
 
reply
    Bookmark Topic Watch Topic
  • New Topic