|
Java SE source code example of Java SWING JSR 296 application -
How to run progress bar and rotating icons in right down corner
Many years ago when SUN created SWING GUI library and added it to JDK,
there were a lot of good documented working examples.
I remember really good tutorials "How to use Tables in SWING",
"How to use this, how to use that, etc."
Now we have different situation.
It was surprising, but it was difficult to found good examples of working applications
of new 'JSR 296' standard SWING.
There is a good template in Netbeans (6.7.1 and 7.0M1) for Java Desktop application.
Using Netbeans wizard it is easy to create an application with some pre-installed features.
But what to do next - how to use this template?
For example - how to run progress bar and rotating icons in right down corner
of the main window.
These indicators should show progress of the running task.
This is an example of Java SWING application.
It works under Netbeans 6.7.1.
There are two additions classes:
1) TaskCenter - generate new tasks, keeps additional information about tasks in the application.
2) LongTask - example of long-lasting task
Pay attention to two additional string, which were added to Netbeans's View class
of SWING template application:
public DesktopApplicationJsr296View(SingleFrameApplication app) {
...
...
///////////////////////
TaskCenter taskCenter = TaskCenter.getInstance();
taskCenter.setTaskMonitor(taskMonitor);
///////////////////////
}
After compilation in Netbeans this simple application will show
you usage of the 'busy animation' in the status bar.
Click on 'New Task' menu item and you will see the progress.
Zipped directory with working copy of Netbeans project example with 'busy animation' in the status bar.
java_se_examples_code/DesktopApplicationJsr296.zip
- Java SE SWING JSR 296 Aplcation with 'busy animation' in the status bar.
JavaDoc for - Java SE SWING JSR 296 Aplcation
with 'busy animation' in the status bar.
Source code of classes TaskCenter.java and LongTask.java :
package com.robohobby.jsr296;
import org.jdesktop.application.ApplicationContext;
import org.jdesktop.application.TaskMonitor;
import org.jdesktop.application.TaskService;
@author
public class TaskCenter {
private static TaskCenter taskCenter = new TaskCenter();
private TaskCenter()
{
}
public static TaskCenter getInstance()
{
return taskCenter;
}
private static int taskCounter = 0;
TaskMonitor taskMonitor;
public TaskMonitor getTaskMonitor() {
return taskMonitor;
}
public void setTaskMonitor(TaskMonitor taskMonitor) {
this.taskMonitor = taskMonitor;
}
public void runNewTask(int maxStep, long msecDelay) {
if (taskMonitor != null) {
LongTask longTask = new LongTask();
longTask.setTaskName( "Task #" + taskCounter );
longTask.setMaxStep(maxStep);
longTask.setMsecDelay(msecDelay);
taskCounter++;
ApplicationContext applicationContext = DesktopApplicationJsr296.getApplication().getContext();
TaskService taskService = applicationContext.getTaskService();
taskService.execute(longTask);
taskMonitor.setForegroundTask(longTask);
}
}
}
package com.robohobby.jsr296;
import org.jdesktop.application.Task;
@author
public class LongTask extends Task<Void, Void> {
private int maxStep = 0;
private long msecDelay = 0;
private String taskName;
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public int getMaxStep() {
return maxStep;
}
public void setMaxStep(int maxStep) {
this.maxStep = maxStep;
}
public long getMsecDelay() {
return msecDelay;
}
public void setMsecDelay(long msecDelay) {
this.msecDelay = msecDelay;
}
LongTask() {
super( DesktopApplicationJsr296.getApplication() );
}
@return
@throws
@Override
protected Void doInBackground() throws Exception {
for(int i = 0; i < this.maxStep; i++) {
System.out.println("LongTask.doInBackground() "
+ " taskName="+taskName+" taskCounter i="+i);
setMessage("Doing step #"+i);
Thread.sleep(msecDelay);
setProgress(i, 0, this.maxStep-1);
}
Thread.sleep(msecDelay);
return null;
}
}
| |