Home of www.RoboHobby.com Java robotics project www.CubesAndCrosses.com
Train your logic skills in geometry
Create and delete one-color crosses
Play for free right in your browser!
Home-made CNC machine
Our new project -
How to make home-made CNC router
PICkit2 - Tutorials, Experiments
PICkit 2 - Small size USB programmer
from Microchip.
How to use it as a demo device for
USB programming in C language




www.RoboHobby.com
Home of www.RoboHobby.com
Java robotics project


Portable Notebook Table
How to make Portable Notebook Table.
Do it yourself!

Java SE SWING JSR-296 Example
How to run background task in
JSR-296 SWING application
with 'busy animation' in the status bar.
Useful addition to Netbeans template.

Java ME Code Examples
Java ME Source code examples
for robotics projects


Run Java ME Code Examples
How to run our Java ME code examples
for robotics projects



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.
Long-lasting task in Java SWING JSR-296 application. Practical working example.


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;

/**
 * Class works with JSR 296 Tasks - generate new tasks,
 * keeps additional information about tasks in the application.
 *
 * @author Oleg
 */
public class TaskCenter {

    //Singleton:

    private static TaskCenter taskCenter = new TaskCenter();

    private TaskCenter()
    {
    }

    public static TaskCenter getInstance()
    {
        return taskCenter;
    }
    ////////////////////////////

    //Current number of task, used as ID for new task
    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);

            //Now this task is set as foreground task, that means you can see

            //visual indication for this task
            //for this taks only (!)
            //Any other tasks become invisible in main window of the JSR 296 application.
            taskMonitor.setForegroundTask(longTask);
        }
    }

}


package com.robohobby.jsr296;
import org.jdesktop.application.Task;

/**
 * Example class for long lasting task, which should work
 * as a background process in behind of SWING GUI in JSR 296 application.
 *
 * Works from 0 to maxStep with msecDelay
 *
 * @author Oleg
 */
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() );
    }

    /**

     * Main method, where you should place your long task

     * It is just simple example. Real task should be much more complicated.

     * @return
     * @throws Exception
     */
    @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;
    }

}






Google
 



Contact us:


My Skype name is oleg_skp
Start Skype text chatStart Skype text chat
View Skype profileView Skype profile
Start Skype voice callStart Skype voice call
Add me to your Skype contact listAdd to Skype contact list
Leave voicemail using SkypeLeave voicemail using Skype
Send a file using SkypeSend a file using Skype
Note: To use these links
the Skype program should
be installed on your computer.
You can load it for free from
their official site:
Contact via Skypewww.Skype.com
------------------