Class ActionExample4
- java.lang.Object
-
- org.jdesktop.application.AbstractBean
-
- org.jdesktop.application.Application
-
- examples.ActionExample4
-
public class ActionExample4 extends Application
An@Actionthat executes a backgroundTask.This example demonstates the use of a background
Task. If an@Actionreturns aTask, it's executed on a worker thread, and monitored by the application framework.When executed, the
ListFilesTaskTaskclass recursively lists all of the files beginning with some root, andpublishesthe files it finds, 10 at a time. A private subclass ofListFilesTaskoverrides theTask.processmethod to update a JList's ListModel with the new files:private class DoListFiles extends ListFilesTask { public DoListFiles(File root) { super(root); listModel.clear(); } @Override protected void process(List<File> files) { if (!isCancelled()) { listModel.addAll(files); } } }The example's
go@Action, keeps a reference to theDoListFilesbackgroundTaskso that thestop@Actioncan cancel it:private Task doListFiles = null; @Action public Task go() { stop(); // maybe cancel pending Task doListFiles = new DoListFiles(getRootFile()); setStopEnabled(true); return doListFiles; } @Action(enabledProperty = "stopEnabled") public void stop() { if ((doListFiles != null) && (doListFiles.cancel(true))) { setStopEnabled(false); } }TheAction'sresources are initialized from a ResourceBundle, as withActionExample2. Additionally, theListFilesTask'stitleanddescriptionproperties are initialized from theresources/ListFilesTask.propertiesResourceBundle:ListFilesTask.title = List Files ListFilesTask.description = List all of the files accessible from some root directory ListFilesTask.directoryMessage = Listing files in {0}ThedirectoryMessageresource is used byListFilesTaskto format amessageeach time a new directory is listed.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.jdesktop.application.Application
Application.ExitListener
-
-
Constructor Summary
Constructors Constructor Description ActionExample4()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Taskgo()Thego@Action.booleanisStopEnabled()static voidmain(java.lang.String[] args)voidsetStopEnabled(boolean stopEnabled)protected voidstartup()Responsible for starting the application; for creating and showing the initial GUI.voidstop()Thestop@Action.-
Methods inherited from class org.jdesktop.application.Application
addExitListener, end, exit, exit, getContext, getExitListeners, getInstance, getInstance, hide, initialize, launch, quit, ready, removeExitListener, show, shutdown
-
Methods inherited from class org.jdesktop.application.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
-
-
-
-
Method Detail
-
go
@Action public Task go()
Thego@Action.Cancel the pending DoListFiles Task and then return a new one. We add a PropertyChangeListener to the new Task so that we can monitor its "message" property.
- Returns:
- the new background Task
- See Also:
stop()
-
stop
@Action(enabledProperty="stopEnabled") public void stop()
Thestop@Action.Cancel the pending DoListFiles Task, if there is one.
- See Also:
go()
-
isStopEnabled
public boolean isStopEnabled()
-
setStopEnabled
public void setStopEnabled(boolean stopEnabled)
-
startup
protected void startup()
Description copied from class:ApplicationResponsible for starting the application; for creating and showing the initial GUI.This method is called by the static
launchmethod, subclasses must override it. It runs on the event dispatching thread.- Specified by:
startupin classApplication- See Also:
Application.launch(java.lang.Class<T>, java.lang.String[]),Application.initialize(java.lang.String[]),Application.shutdown()
-
main
public static void main(java.lang.String[] args)
-
-