Progress Bar

The Swing toolkit provides a separate GUI thread to process user interface interactions. This separate thread should not process long running jobs. Among the long running jobs we find for example initializing the Prolog interpreter and possibly running queries. To get these long run-ning jobs away of the Swing GUI thread we start a new thread.

The new thread will process the long running job in the background and the Swing GUI will remain responsive. So that only one long running job is executed one at a time, we will disable our own user interface pane and show an indeterminate progress bar. The background job will then enable our own user interface pane as soon as it has finished:

    /**
* <p>While the knowledge base is loading or a query
* is processed we use the following layout with the
* buttons disabled:</p>
* <pre>
* Firstname: [ ]
* Name: [ ]
* Age: From [ ] To [ ]
* Salary: From [ ] To [ ]
* ( Debug ) ( Search )
* +------------ Progress Bar ------------+
* | |
* | |
* +--------------------------------------+
* <pre>

For this job processing the user interface pane provides the following method. We only sketch the main parts of this method. For more details on this method we recommend consulting the source code. The background job is simply started as an out of the box Java thread via first creating it and then starting it. When the job has completed it will use a Swing utility to post a custom even that will reset the user interface pane:

    /**
* <p>Start a job.</p>
*
* @param job The long running job.
* @param job2 The GUI update job.
*/
public void startJob(final Runnable job, final Runnable job2) {
new Thread((Runnable) () -> {
job.run();
SwingUtilities.invokeLater(() -> {
job2.run();
});
}).start();

To switch between the list and the progress bar we use a card layout. This layout allows stack-ing different Swing components one on top of another. Only the top card will be visible and an arbitrary card can be selected as the top card. The layout will automatically do revalidation and repainting. The layout has the further advantage that it is compatible with packing the main frame to get a computed size.

Kommentare