File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Is JFrame added to JFrame heavyweight or lightweight? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Is JFrame added to JFrame heavyweight or lightweight?" Watch "Is JFrame added to JFrame heavyweight or lightweight?" New topic
Author

Is JFrame added to JFrame heavyweight or lightweight?

Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
In http://java.sun.com/products/jfc/tsc/articles/mixing/index.html
<blockquote>A lightweight component is one that "borrows" the screen resource of an ancestor (which means it has no native resource of its own -- so it's "lighter").
</blockquote>

and further
<blockquote>
all Swing components are lightweight (except for the top-level ones: JWindow, JFrame, JDialog, and JApplet)
</blockquote>

If JFrame instance added to JFrame window, then is it heavyweight or lightweight?

[This message has been edited by G Vanin (edited November 12, 2001).]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi G,
The reason that JFrame, JDialog, JWindow, and JApplet are heavyweight is because the OS draws the outside of them and the OS link must be created and maintained. The components are handled by the OS windowing environment for such things as iconification, maximize, minimize, resize, top/bottom/middle placement, etc.
The OS functionality can not be changed from within Swing for top level components because of the OS link that MUST exist.
Therefore, any time a Swing top-level component is used in a GUI we are using heavy-weight components. The advantage of Swing is that all other Swing components are lightweight and therefore can be customized from within Swing. That is why buttons can look the same no matter what OS is used.
Regards,
Manfred.
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
Thanks, Manfred man,
for clearing me the fact asked.
But logically it was not clear to me.
<blockquote>
the OS link must be created and maintained
</blockquote>
OK, but for a link/reference/pointer, why there is a need for peer (from OS)?
<blockquote>
The OS functionality can not be changed from within Swing for top level components because of the OS link that MUST exist.
</blockquote>
OK for (external) container but why it may not be broken for internals?
In this connection I have a curiosity:

  1. How it is determined inside Swing classes that some may be put directly to desktop while others only to container?


  2. Why it is impossible to get rid of peers, providing just a link, native API interfacing (for iconizing, etc) and drawing directly, i.e. w/o using heavy-weght counter-part peer?

  3. [This message has been edited by G Vanin (edited November 12, 2001).]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You can't add a JFrame to a JFrame. The reason top-level components are called top-level components is because they are the top-level of the GUI. You can't add a JFrame or any other top-level component inside another container.

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
Thanks Nathan,
that was the most difficult part
Arnaud Berger
Greenhorn

Joined: Nov 09, 2001
Posts: 6
Originally posted by Manfred Leonhardt:
Hi G,
The reason that JFrame, JDialog, JWindow, and JApplet are heavyweight is because the OS draws the outside of them and the OS link must be created and maintained. The components are handled by the OS windowing environment for such things as iconification, maximize, minimize, resize, top/bottom/middle placement, etc.
Regards,
Manfred.

Hi !
Are you sure JApplet is heavy-weight ?
Hierarchy is Panel-->Applet-->JApplet
so, it shouldn't be ?
Arnaud
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
That <blockquote>
all Swing components are lightweight (except for the top-level ones: JWindow, JFrame, JDialog, and JApplet)
</blockquote> was a citation (citations were shifted a little bit to the right in my post) from that cited Sun's link
It is just a first paragraph in section "Heavy vs. light: the differences" inside that link/page
I do not take responsibilities for others, sorry. But I would like to have some more links since I have more questions in this area. For ex.,
  1. in AWT all components are heavy-weight but we can add some (or any? which?) into containers. In Swing top-level is synonym of heavy-weight. Correct? So this logic, that the component may not be added because it is top-level, i.e. heavy-weight, is blurred to me

  2. I also asked somewhere in this forum and did not get to answer:
    what is determining that some component may or may not be shown directly by OS. How to check this in the source?
    what is determining that some component may or may not be added to container?How to check this in the source of that class?

  3. [This message has been edited by G Vanin (edited November 15, 2001).]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121


Are you sure JApplet is heavy-weight ?
Hierarchy is Panel-->Applet-->JApplet
so, it shouldn't be ?


Both Panel and Applet are AWT widgets and are heavyweight. JApplet is a special case. Applets were made to be embedded into web-pages, so they need to have a native peer to interact with web browsers ( which are usually written in native code ). Applets are heavy weight, but are not necessarily top-level. They can be thought of as top-level when they are running in a browser, because no Java object contains them... but since they inherit from Panel, they can also be contained within other components.

OK... to clear things up I'll define what I mean by the terms top-level and heavy-weight.

  • Top-Level - Root of the GUI heirarchy, top-level components cannot be contained within other components.
  • Heavy-Weight - Component has a native peer to communicate with the underlying operating system.


  • In AWT, all components were heavy-weight, but only Window, Frame, Dialog, and (sometimes) Applet (and any classes extending these) were top-level. In Swing, JWindow, JFrame, JDialog, and (sometimes) JApplet (and any classes extending them) are heavy-weight, and are also top-level. Applet and JApplet are special cases, as mentioned previously. They are always heavy-weight, but are not always top-level.

    I am not sure how you can check to see if a component is top-level or heavy-weight in the code, other than by looking at what the class extends. Just remember, in AWT all components are heavyweight, and Window, Frame, and Dialog ( and any classes extending them ) are top-level only. In Swing, JWindow, JFrame, JDialog, and JApplet ( and any classes extending them ) are heavy-weight, and JWindow, JFrame, and JDialog( and any classes extending them ) are top-level only.

    -Nate
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
Nathan,
thanks a lot, once more.
I would not have written the following, hadn't I had a great confusion due to difficulties in understanding terminology.
When I see "Top-Level - Root of the GUI heirarchy", I cannot help but having in my head java.lang.Object because, IMHO, by default/omission the hierarchy is always implied with inheritance sense. In other sense the hierarchies are usually qualified.
It is really hard (for me) to shift in my head smth I am accustomed to. You meant "containment hierarchy", right?
It is better if you qualify hierarchies in this forum, please.
Widget is not Java (and I blieve OO) term. I may guess that it is a component. Correct? General or some specification?
 
 
subject: Is JFrame added to JFrame heavyweight or lightweight?