Optimization of java.awt.Component's initialization

Author: Dafe Simonek
$Revision: $
$Date: 2009/10/29 16:50:16 $
Document history: available in CVS

What is this technique good for

Use following technique when init of your component takes noticeable amount of time so that it slows down init of some dialog or frame significantly.

Main trick is to defer expensive parts of initialization to later time. Later time here is defined as the time right after first paint, when paint event arrives. Majority of the init time is often spent in filling the component with data or widgets that are not likely to be used in first seconds of use anyway, so we can safely load and draw them later, not in first paint.

Things like explaining descriptions, long lists in combo boxes, large background images are good candidates for their later loading.

Implementation steps:

1. Identify expensive parts of code which could be loaded later, after first paint. Use profiling techniques article tips to profile dialog ir frame initialization code.

2. Implement org.openide.util.AsyncGUIJob interface.
Convenient way is to add implementation of AsyncGUIJob right into code of component in question. See javadoc documentation of AsyncGUIJob to find details about how and when methods in your implementation will be called.

3. Optionally implement org.openide.util.Cancellable interface.
Implement Cancellable interface in the case you want to allow the system to cancel your initialization task. System will call cancel method when component stops to be showing during its initialization phase. Cancel ability is desirable for longer lasting init tasks like some network connections. Note that implementation of AsyncGUIJob Cancellable interfaces must be present in the same class to be usable for org.openide.Utilities.attachInitJob, read below.

4. Attach init job to component using org.openide.Utilities.attachInitJob(Component comp4Init, AsyncGUIJob initJob).
Pass your component to be inited as first param, as second param pass implementation of init job, optionally accompanied by Cancellable implementation.

5. Show your component as usual
Further work with component is unchanged, insert your component into AWT hierarchy and show in dialog, wizard, frame, whatever, just like you do with other components.


See java source org.netbeans.core.ui.IDESettingsPanel.java for example of described technique.

UI notes:

Your component now has at two states in its visual lifecycle, "initially drawn" and "fully drawn". Remember to not change dimensions, colors, shapes during transition from first state to second. Goal is to make the transition as hidden and not intrusive as it is possible.

Also, don't forget to put some "initializing" or "loading" labels on parts of UI which init is long enough to be noticeable by user (aroung or greater then 1 second). For longer running initialization support user feedback by using busy cursor over affected subcomponents. See article Howto use wait and progress cursors

Please send comments to nbdev.

Project Features

About this Project

Performance was started in November 2009, is owned by tpavek, and has 27 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20160708.bf2ac18). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
Please Confirm