Equivalent of BusyIndicator [message #1730710] |
Wed, 27 April 2016 21:11 |
|
My e4 app is a client-server app, so there are several operations that you wouldn't really call long-running, but which take long enough that it would be nice to give feedback to the user that something is happening. For example, opening an editor means downloading the contents from the server, which will take 1 or 2 seconds. Generally speaking, anything that takes longer than a second should give some feedback.
Is there an e(fx)clipse equivalent of SWT's BusyIndicator.showWhile()? Doing the equivalent in JavaFX is somewhat complicated, involving a combination of Tasks and Platform.runLater(), so I was hoping someone had already done the work
Colin Sharples
CTG Games Ltd
Wellington, New Zealand
|
|
|
Re: Equivalent of BusyIndicator [message #1730712 is a reply to message #1730710] |
Wed, 27 April 2016 21:49 |
|
Okay, maybe it's not all that much work...
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Cursor;
import javafx.scene.Scene;
public class BusyIndicator {
public static void showWhile(Scene scene, Runnable runnable) {
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
Cursor cache = scene.getCursor();
Platform.runLater(() -> scene.setCursor(Cursor.WAIT));
try {
runnable.run();
} finally {
Platform.runLater(() -> scene.setCursor(cache));
}
return null;
}
};
new Thread(task).start();
}
}
Colin Sharples
CTG Games Ltd
Wellington, New Zealand
|
|
|
Re: Equivalent of BusyIndicator [message #1730713 is a reply to message #1730710] |
Wed, 27 April 2016 21:48 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
Hi,
e(fx)clipse has no such thing in its library and I'm not aware of
another JavaFX library who has such a feature.
I would probably target it a bit differently anyways and instead open
the editor immediately and showing the load progress / wait info inside
the editor area but anyways:
In general I understand the need for such a feature and think
e(fx)clipse should provide a service-API one can use. Feel free to file
a bug and maybe contribute an implementation.
BTW - I'm more a fan of JDKs concurrent-APIs when we talk about
asynchronous programming!
Tom
On 27.04.16 23:11, Colin Sharples wrote:
> My e4 app is a client-server app, so there are several operations that
> you wouldn't really call long-running, but which take long enough that
> it would be nice to give feedback to the user that something is
> happening. For example, opening an editor means downloading the contents
> from the server, which will take 1 or 2 seconds. Generally speaking,
> anything that takes longer than a second should give some feedback.
>
> Is there an e(fx)clipse equivalent of SWT's BusyIndicator.showWhile()?
> Doing the equivalent in JavaFX is somewhat complicated, involving a
> combination of Tasks and Platform.runLater(), so I was hoping someone
> had already done the work :)
|
|
|
Re: Equivalent of BusyIndicator [message #1730716 is a reply to message #1730712] |
Wed, 27 April 2016 23:21 |
|
Thanks, Tom. Given that there is an Eclipse/SWT version, I thought it would be nice to support something similar in e(fx)clipse. I have tweaked it a bit, as I realized it wasn't quite doing what I wanted. To be clear, the point about this is to provide the ability to manage short long-running tasks, so while it is using a Task to invoke the action, it's really about providing feedback during an action that would normally just be invoked on the FX app thread. So the tweaks I made are: moved the set WAIT cursor out of the task, so that will happen immediately on the return from showWhile(), and invoked the runnable on the FX application thread using Platform.runLater(), so that any work you perform can do UI work directly without invoking Platform.runLater(). The sweet spot for using this is for user actions that will take between 1 and 3 seconds to run - anything shorter than a second doesn't really need to provide feedback, anything longer than 2-3 seconds should probably stronger feedback such as a progress bar, and use a genuine background task to do the work (for which the JavaFX concurrent support is enough). I'll raise a bug for this.
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Cursor;
import javafx.scene.Scene;
public class BusyIndicator {
public static void showWhile(Scene scene, Runnable runnable) {
Cursor cache = scene.getCursor();
scene.setCursor(Cursor.WAIT);
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
try {
Platform.runLater(runnable);
} finally {
Platform.runLater(() -> scene.setCursor(cache != null ? cache : Cursor.DEFAULT));
}
return null;
}
};
new Thread(task).start();
}
}
Colin Sharples
CTG Games Ltd
Wellington, New Zealand
|
|
|
Powered by
FUDForum. Page generated in 0.02892 seconds