Optimization of java.awt.Component's initializationAuthor: Dafe Simonek
$Revision: 22.214.171.124 $
$Date: 2009/10/29 16:50:16 $
Document history: available in CVS
What is this technique good forUse 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.
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.
Convenient way is to add implementation of
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
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
must be present in the same class to be usable for
4. Attach init job to component using
org.openide.Utilities.attachInitJob(Component comp4Init, AsyncGUIJob
Pass your component to be inited as first param, as second param pass implementation of init job, optionally accompanied by
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.
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.