Khangharoth

May 7, 2007

Check,Debug your Swing App

Filed under: Java — khangharoth @ 11:54 am

If you are building a desktop application and using Java as programming platform ,than you must already be aware of these problems.If you are facing problems like GUI freezing or dead look in Swing event dispatch thread and this results in arbitrary painting/repainting problems.

Firstly their should be no mixing of AWT/Swing components.Now Swing has evolved a lot and so their should be no excuse to use AWT components,Only if you are making a Graphics application and using things like JOGL ,GLCanvas than it makes sense to still use AWT GLCanvas rather than Swing GLJPanel.

Before problems,lets just quickly sums up responsibilities of Swing Thread.

i) Painting/Repainting of the Java Components.

ii) Fire Events.

iii) Your App work,if you have placed your application logic in some action-perform method.This is one area you should be careful,if it takes time to execute (even 100ms) your Gui will start behaving unresponsive.You can use things like SwingWorker ,SpinOff,Foxtrot etc.

If mixing of AWT/Swing is not the problem and still your Gui is giving you some problems.Let’s try to list down some common problems in a Swing based Gui.

1) Gui just hangs and if you do any operation like resize etc,everything is just gone.Even if you wait for some some time ,it never comes to life.This means some deadlock has occurred to the Swing thread.

a) Check if you are taking any Lock in Swing Event thread.This is one of the most common mistake.

b)If you are doing some I/O or network operation from some action-perform method.

c) Also i have seen some junior programmers directly write data-base interaction code in action-perform method.This strategy can give you problems like ,running fine on our environment but on Client everything just hangs.

2) Gui hangs momentarily and then repaints itself.This is easy to debug as you are doing some time-consuming task in Swing thread and so its painting mechanism has suffered.

As Swing is not designed for Fail First approach ,most of the problems remain buried under when you have small Gui.The moment your Gui code becomes appreciable ,all these problems start popping out at unexpected times.

So the best strategy is to make your Swing development as Fail-First . Well this means the moment you have done something which violates the best practices,your program should fail rather than cover up this small problem.

You can use these two Classes to constantly monitor health of your Swing Gui.

  • EventDispatchThreadHangMonitor.java
  • CheckThreadViolationRepaintManager.java

I found these utilities on net and using them has helped me a lot.

How to use them? —-> Include this is your package Structure and Just insert these two line in your start of the application.

EventDispatchThreadHangMonitor.initMonitoring();
RepaintManager.setCurrentManager(new CheckThreadViolationRepaintManager());

Swing has really came a long way!

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: