Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Tip on Mixing Heavyweight and Lightweight Java Components

By Laura Bennett
2003-10-01


The answer

 

Z-order refers to how objects are layered on top of each other in a container. If an object overlays part of another control in the same container, then it is in a different layer. The layers are known as x, y, and z, thus giving us the name z-order. You can purposely set the z-order; however, I found this did not solve the problem described above. So I had to dig a little deeper.

When Java APIs were first introduced, the AWT classes consisted of mostly heavyweight objects. (One exception to this was the Component class.) The objects are heavy because they require the simultaneous creation of what is known as a peer (that is, a platform-dependent object) in order to be displayed on the screen. The peer, also known as a native window, changes based on which platform you are running on. On the other hand, most Swing classes that extend AWT do not make use of a peer object so they don't carry the baggage that the original AWT classes do; hence the name lightweight. A lightweight component is not associated with a native opaque window. Therefore, no matter where you run your program, you will get the same look and feel, and most importantly there is no native code involved. In other words, if you use a non-Swing button, you will get a button whose look is based on the operating system (OS); whereas, if you use a Swing button, you will get a drawing of an object that appears to be a button independent of the OS. It is also important to point out that even though some of the classes make use of native OS code, this does not inhibit an application's portability. The native code is built into the Java run-time environment, not your application.

You may have noticed that I said most Swing classes that extend AWT do not make use of a peer. JFrame does not follow this model. JFrame extends the Frame class, which is a heavyweight object. Consequently, JFrame is a heavyweight object as well. Other Swing classes that are heavyweight are JWindow, JDialog, and JApplet.

All Swing components are derived from AWT components. For example, if you travel up the hierarchical tree, you will see JLabel is derived from JComponent, which is derived from AWT Component. The AWT Component is a lightweight object so therefore, the JLabel is lightweight. Label also subclasses the Component class, so it too is considered lightweight.

The problem described above involves a Panel that is embedded in a heavyweight Frame object. The popup in the JCombobox is lightweight. Consequently, the Frame component is in the way of the JCombobox's popup, thus hiding it in the background. The native peer window that is created with the Frame component is opaque, so it can't be used to implement transparent areas.



Tutorial Pages:
» The Problem
» The answer
» The resolution
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT