Friday, March 12, 2010

JDK6u12 - Mixing heavy and lightweight component

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");
jc.addItem("France");
jc.addItem("Germany");
jc.addItem("Italy");
jc.addItem("Japan");

add(jc);
add(btn1);
add(btn2);
add(btn3);
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new Test());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}



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


For more: Please join us at Tech Days at Hyderabad on 24-25th of March.

No comments: