Showing posts with label Swing. Show all posts
Showing posts with label Swing. Show all posts

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.

Monday, February 18, 2008

Play Safe with Swing

Last month, I was writing a GUI based code in Java. As Swing provides rich UI so I decided to go for Swing. Here is a simple piece of Suggestion, which most of us know already. If you are a novice in Swing, you always need to take care in calling UI work. Reason is simple, swing is not thread-safe and calling GUI work randomly will lead to a deadlock in code. We are seeing 'n' no. of problem with Swing code, just because developers don't know(or maybe forget) that swing is not a thread-safe world. Sometime you are not able to see the problem initially but afterward when your code is going heavier and heavier you will face lot of problems.

How to make Swing Thread-safe ?

Its not a tough job. First see this, this is more or less like a rule and you can apply anywhere to write a safe code:

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.show();
// Anything after this is going to be crappy and thread unsafe
}

If you are going to write two UI work, and since things are not synchronized, you can go into a big mess. I wanted to post my code where I got the problem some days back, but its too big to post. No worries I got a code from here which is amazing ! You know what is cool, it crashes every time :)

import javax.swing.*;
import java.awt.*;

public class StrangeProblem extends JFrame {
static {
new StrangeProblem();
}

private static void staticMethod() {
System.out.println("This is never reached");
}

private StrangeProblem() {
getContentPane().add(new MyJDesktopPane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setVisible(true);
// If commented out, program works fine, otherwise it hangs
new JInternalFrame();
}

private class MyJDesktopPane extends JDesktopPane {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("We will now call the static method...");
staticMethod();
System.out.println("Static method was called.");
}
}

public static void main(String[] args) {
}
}

(Thanks for the poster of this code)



So now you got the thumb rule, what not to do ! Alright time to see what to do.

Simple. Run your code in the event-dispatching thread. Most of the UI like event, mouse clicks are always handled by event-dispatching thread. SwingUtilities class provide two methods invokeLater() and invokeAndWait() to get rid of this problem. Now, why two different methods is a big mystery, but use invokeLater() if its a case of Swing. So, now the code skeleton is :

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.show();
// you can write more UI work here
}
});
}

I can leave it on to you to make the above code deadlock free :). For more detail, see the SwingUtilities class JavaDoc.