Friday Mar 26, 2010

Tech Days 2010 @ Hyderabad

2010 Sun Tech days over. Nice time at presentation and in train too. Nothing more to write. James was good. 

Presentation here: 

 G1 presentation  and mixing Lightweight/Heavyweight components.

Tuesday Dec 15, 2009

Lightweight and Heavyweight components - Mess it !

So, if you are a Swing Developer, you have heard many stories where someone messed 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")."

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

[Read More]

Wednesday Jun 04, 2008

Swing to FX converter

Don't know what made me to think on this. But JavaFX seems to be the next hit of Sun Family. JavaFX is dealing with Rich Internet Application and Content based UI. Being a scripting language ,coding is considered to be easy but as a developer I personally think in Java Frame of Mind coding. This probably let me think on this. Is there any chances that we can converter all the existing Swing or UI application into JavaFX. Probably most of the Java UI application base is written in Swing and Applet, can we make a tool where we can convert existing application into FX application + those programmers who are not feeling very comfortable with JavaFX(like me :D) can also make stuff in JavaFX style.

I remember some 3-4 years back, there was a similar situation with AJAX. Being using JavaScript, developers are finding it difficult to write application in AJAX and then Google came with its toolkit called GWT which gives the developer flexibility to write code in Java and rest will be taken care by Google Toolkit. And now, I can say AJAX is one of the major hits. Making such converters always help in capturing market and developer find them self more attached to a programming language. Why only Swing/applet to JavaFX, it can be JS/HTML/CSS to JavaFX.

Talking of implementation, it's quite tough to write API based conversion means converting say Swing API to JavaFX API. One of my colleagues gave me a quite better idea to grab the output and then do the conversion. Means, run a Swing written application, pass the PID into our converter and then generate the JavaFX code. It is tough but not too much. At least in Java, we have supports of things like accessibility API's which can help us in doing this.

Please let us know your point of view.

Monday May 12, 2008

Nimbus Look And Feel

Good news for all the user, using JDK6. Though I am little late in
writing blog on this but its OK ;). JDK6 current update comes with a
new L&F(Look And Feel) called Nimbus.

Nimbus provide more lively look and feel in Swing UI. Here are some examples:

Default Look And Feel(Click to see enlarged mode)
Nimbus Look And Feel((Click to see enlarged mode)

the image is little small and related to my Online Java Project(which I
can't change), so I provide another example here from my last blog code.

Default Look And Feel
Nimbus Look And Feel

more interesting is focus traversal on components. For Default L&F,
you can see that there is a dotted rectangle on Button 3, which says
"Focus is here" whereas in Nimbus you can see Button 1 with little
bluish highlight which says "Look at my focus" :)

Quickest way to try, command line:

Run my previous (or any UI code) with the following option:

java LayoutCheck

Off course, you can do it with code :

Use try, catch and respect Exception handling as well.

Tuesday Apr 22, 2008

Swing Framework Presentation

Weeks back, I had made one small presentation on Swing Framework which basically covers JSR 296. Here is the link to download the presentation.

For great detail, see the technical article on java.sun site:

If you want to run the codes, without Netbeans support, do the following steps:

1. Go on to this link: and take the source code of the project -
Source code:

2. Unzip it and compile it. Compilation will be easier in Netbeans as it makes jar file for you.

3. Run the code from command line: 

javac -cp  E:\\SwingFramework.jar          

(where  E:\\SwingFramework.jar is the location of jar file, we made in Step 2)

java -cp  E:\\SwingFramework.jar;. Ex01

NOTE: Best is to use Netbeans5.5+ :) 

Monday Feb 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();;
// Anything after this is going to be crappy and thread unsafe

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());
setSize(300, 300);
// If commented out, program works fine, otherwise it hangs
new JInternalFrame();

private class MyJDesktopPane extends JDesktopPane {
protected void paintComponent(Graphics g) {
System.out.println("We will now call the static method...");
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.

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();;
// 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.


Vaibhav Hi, I am Vaibhav Choudhary working in Oracle with JDK team. This blog is all about simple concept of Java, JVM and JavaFX.


« July 2016