Java Reference
In-Depth Information
stage.setTitle("Service Example");
stage.setScene(view.scene);
stage.show();
}
private void hookupEvents() {
view.startButton.setOnAction(actionEvent -> {
model.shouldThrow.getAndSet(false);
((Service) model.worker).restart();
});
view.cancelButton.setOnAction(actionEvent -> {
model.worker.cancel();
});
view.exceptionButton.setOnAction(actionEvent -> {
model.shouldThrow.getAndSet(true);
});
}
private static class Model {
public Worker<String> worker;
public AtomicBoolean shouldThrow = new AtomicBoolean(false);
public IntegerProperty numberOfItems = new SimpleIntegerProperty(250);
private Model() {
worker = new Service<String>() {
@Override
protected Task createTask() {
return new Task<String>() {
@Override
protected String call() throws Exception {
updateTitle("Example Service");
updateMessage("Starting...");
final int total = numberOfItems.get();
updateProgress(0, total);
for (int i = 1; i <= total; i++) {
if (isCancelled()) {
updateValue("Canceled at " + System.currentTimeMillis());
return null; // ignored
}
try {
Thread.sleep(20);
} catch (InterruptedException e) {
if (isCancelled()) {
updateValue("Canceled at " + System.currentTimeMillis());
return null; // ignored
} }
if (shouldThrow.get()) {
throw new RuntimeException("Exception thrown at " +
System.currentTimeMillis());
}
Search WWH ::




Custom Search