Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » e(fx)clipse » Equivalent of BusyIndicator
Equivalent of BusyIndicator [message #1730710] Wed, 27 April 2016 21:11 Go to next message
Colin Sharples is currently offline Colin SharplesFriend
Messages: 96
Registered: July 2009
Location: Wellington, New Zealand
Member

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 Smile


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 Go to previous messageGo to next message
Colin Sharples is currently offline Colin SharplesFriend
Messages: 96
Registered: July 2009
Location: Wellington, New Zealand
Member

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 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
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 Go to previous message
Colin Sharples is currently offline Colin SharplesFriend
Messages: 96
Registered: July 2009
Location: Wellington, New Zealand
Member

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
Previous Topic:Using e(fx)clipse osgi wrapper inside a maven project
Next Topic:Can't Send Email using SMTP in JavaFX
Goto Forum:
  


Current Time: Wed Apr 24 23:56:09 GMT 2024

Powered by FUDForum. Page generated in 0.02892 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top