Showing Progress without blocking the Entire application
Posted by Pavan Kumar on March 12, 2008
A very common usecase in Gui applications is to show an indeterminate progress bar whenever there is a long running task. Tasks can range from short ones such as data fetch to long ones such as a data migration or complex computations. In case of long running tasks it makes sense to show progress, but at the same time allow the user to perform other actions or work on other screens parallely ,within the application.
Swing allows us to achieve this functionality rather easily.
The demo below shows this functionality.
The overlay progress is shown whenever the tree is expanded and the nodes are loaded using swingworker, while u can continue to work on the table on the right .
The OverlayProgressContainer component enables us to do just this.
1: It makes use of the lesser known but very helpful Overlaylayout to stack components .
2: It has 2 children
a) User defined panel added as the bottom most child
b) GlassPanel added as the top most child .This panel shows progress using the JxBusyLabel and blocks keyboard(using KeyEventDispatcher),mouse events on the user defined panel .
3: The glasspanel is registered as listener to a swing worker and shows the progress based on the states(‘started’, ‘done’) of the swing worker
To make use of this component
1: Set the user defined panel using setDecoratedComponent(JComponent component)..
2: Set the swingWorker so that glasspanel can registered itself using setSwingWorker(SwingWorker worker)
Note: SwingWorker can be used only once and hence has to be created again once it has finished executing.