Vaibhav's Blog Space

  • Java
    December 15, 2009

Lightweight and Heavyweight components - Mess it !

Guest Author

So, if you are a Swing Developer, you have heard many stories where someone messed up Lightweight component with Heavyweight component. In one line " A heavyweight component
is one that is associated with its own  native screen resource (commonly
known as a peer). 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")." AWT is all heavyweight, Swing is all lightweight except top level ones like JFrame, JWindow...

Now many times you have heard "Don't mix lightweight and heavyweight". What will happen ? Alright, here is a small code :


import javax.swing.\*;
import java.awt.\*;
public class Test extends JPanel {
  public Test() {
     JComboBox jc = new JComboBox();
     JButton btn1 = new JButton("Button1");
     Button btn2 = new Button("Button2");
     Button btn3 = new Button("Button3");
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new Test());
    frame.setSize(200, 200);

Here what you see the output:

Now, this is what it happen what you mix lightweight and heavyweight. No way, you can bring the drop down items on top of Button2 !!

All Past :) , JDK6 update 12 and JDK7 build 19, the output will be like this:

 New JDK release fixed all these problem of mixing lightweight component and heavyweight component. So, don't worry, keep messing :).

For more detail, please see this : http://java.sun.com/developer/technicalArticles/GUI/mixing_components/index.html

Join the discussion

Comments ( 29 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.