Hallo!
Ich habe eine SingleFrameApplication wo ich einen TaskMonitor regestriere.
Wenn ich nun einen Task ausführe, so wird der PropertyChangeListener zwei mal aktiviert.
Also er kommt zwei mal in "started" und am Ende zweimal in "done".
Hier das Beispielprogramm:
Output:
Ich habe eine SingleFrameApplication wo ich einen TaskMonitor regestriere.
Wenn ich nun einen Task ausführe, so wird der PropertyChangeListener zwei mal aktiviert.
Also er kommt zwei mal in "started" und am Ende zweimal in "done".
Hier das Beispielprogramm:
Java:
package swing;
import org.jdesktop.application.Application;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.Task;
import org.jdesktop.application.TaskMonitor;
public class TaskMonitorTest extends SingleFrameApplication
{
/**
* At startup create and show the main frame of the application.
*/
@Override
protected void startup()
{
FrameView main = new MainFrame(this);
show(main);
}
@Override
protected void shutdown()
{
// The default shutdown saves session window state.
super.shutdown();
// Now perform any other shutdown tasks you need.
}
/**
* This method is to initialize the specified window by injecting resources. Windows shown in our application come
* fully initialized from the GUI builder, so this additional configuration is not needed.
*/
@Override
protected void configureWindow(java.awt.Window root)
{
}
/**
* Main method launching the application.
*/
public static void main(String[] args)
{
launch(TaskMonitorTest.class, args);
}
public class MainFrame extends FrameView
{
public MainFrame(Application arg0)
{
super(arg0);
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener()
{
public void propertyChange(java.beans.PropertyChangeEvent evt)
{
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName))
{
System.out.println("started");
}
else if ("done".equals(propertyName))
{
System.out.println("done");
}
else if ("message".equals(propertyName))
{
String text = (String)(evt.getNewValue());
System.out.println("test: " + text);
}
else if ("progress".equals(propertyName))
{
System.out.println("progress");
}
}
});
TestTask test = new TestTask();
System.out.println("Task wird gestartet");
getApplication().getContext().getTaskService().execute(test);
}
private class TestTask extends Task<Void,Void>
{
public TestTask()
{
super(Application.getInstance());
}
@Override
protected Void doInBackground() throws Exception
{
setMessage("Opening the file");
Thread.sleep(2000);
System.out.println("im Task");
return null;
}
@Override
protected void finished()
{
super.finished();
}
}
}
}
Output:
Code:
Task wird gestartet
started
started
test: Opening the file
im Task
done
done