Last active
December 10, 2015 16:09
-
-
Save jewelsea/4459216 to your computer and use it in GitHub Desktop.
Samples for returning user objects types and exception types from JavaFX service calls.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import javafx.application.Application; | |
import javafx.concurrent.*; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Pos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.*; | |
import javafx.scene.layout.VBox; | |
import javafx.scene.text.TextAlignment; | |
import javafx.stage.Stage; | |
public class BadFooServiceApp extends Application { | |
public static void main(String[] args) { Application.launch(args); } | |
@Override public void start(final Stage stage) throws Exception { | |
final BadFooGenerator badFooGenerator = new BadFooGenerator(); | |
final Label badFooDisplay = new Label(); | |
badFooDisplay.setMinWidth(250); | |
badFooDisplay.setAlignment(Pos.CENTER); | |
final Button badFooMaker = new Button("Fail Foo"); | |
badFooMaker.setOnAction(new EventHandler<ActionEvent>() { | |
@Override public void handle(ActionEvent t) { | |
badFooDisplay.setText("..."); | |
if (badFooGenerator.getState() == Worker.State.READY) { | |
badFooGenerator.start(); | |
} else { | |
badFooGenerator.restart(); | |
} | |
} | |
}); | |
badFooMaker.disableProperty().bind(badFooGenerator.runningProperty()); | |
badFooGenerator.setOnFailed(new EventHandler<WorkerStateEvent>() { | |
@Override public void handle(WorkerStateEvent t) { | |
Throwable ouch = badFooGenerator.getException(); | |
badFooDisplay.setText(ouch.getClass().getName() + " -> " + ouch.getMessage()); | |
} | |
}); | |
final VBox layout = new VBox(10); | |
layout.setAlignment(Pos.CENTER); | |
layout.getChildren().setAll(badFooMaker, badFooDisplay); | |
layout.setStyle("-fx-padding: 10px; -fx-background-color: cornsilk;"); | |
Scene scene = new Scene(layout); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
class BadFooGenerator extends Service<Foo> { | |
@Override protected Task createTask() { | |
return new Task<Foo>() { | |
@Override protected Foo call() throws Exception { | |
Thread.currentThread().sleep(1000); | |
throw new BadFooException(); | |
} | |
}; | |
} | |
} | |
class BadFooException extends Exception { | |
BadFooException() { | |
super(random.nextInt(100) + ""); | |
} | |
} | |
class Foo { | |
private int answer = random.nextInt(100); | |
public int getAnswer() { | |
return answer; | |
} | |
} | |
private static final Random random = new Random(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.concurrent.Service; | |
import javafx.concurrent.Task; | |
import javafx.concurrent.Worker; | |
import javafx.concurrent.WorkerStateEvent; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Pos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
import java.util.Random; | |
public class FooModifierServiceApp extends Application { | |
public static void main(String[] args) { Application.launch(args); } | |
@Override public void start(final Stage stage) throws Exception { | |
final FooModifier fooModifier = new FooModifier(); | |
final Label fooBeforeDisplay = new Label(); | |
fooBeforeDisplay.setMinWidth(300); | |
fooBeforeDisplay.setAlignment(Pos.CENTER); | |
final Label fooAfterDisplay = new Label(); | |
fooAfterDisplay.setMinWidth(300); | |
fooAfterDisplay.setAlignment(Pos.CENTER); | |
final Button fooMaker = new Button("Make Foo"); | |
fooMaker.setOnAction(new EventHandler<ActionEvent>() { | |
@Override public void handle(ActionEvent t) { | |
fooModifier.reset(); | |
Foo foo = new Foo(); | |
fooModifier.setFoo(foo); | |
fooBeforeDisplay.setText("Before Modification: " + foo.getAnswer()); | |
fooAfterDisplay.setText("..."); | |
if (fooModifier.getState() == Worker.State.READY) { | |
fooModifier.start(); | |
} else { | |
fooModifier.restart(); | |
} | |
} | |
}); | |
fooMaker.disableProperty().bind(fooModifier.runningProperty()); | |
fooModifier.setOnSucceeded(new EventHandler<WorkerStateEvent>() { | |
@Override public void handle(WorkerStateEvent t) { | |
Foo myModifiedFoo = fooModifier.getValue(); | |
fooAfterDisplay.setText("After Modification: " + myModifiedFoo.getAnswer()); | |
} | |
}); | |
final VBox layout = new VBox(10); | |
layout.setAlignment(Pos.CENTER); | |
layout.getChildren().setAll(fooMaker, fooBeforeDisplay, fooAfterDisplay); | |
layout.setStyle("-fx-padding: 10px; -fx-background-color: cornsilk;"); | |
Scene scene = new Scene(layout); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
class FooModifier extends Service<Foo> { | |
private Foo foo; | |
void setFoo(Foo foo) { | |
this.foo = foo; | |
} | |
@Override protected Task createTask() { | |
return new FooModifierTask(foo); | |
} | |
private class FooModifierTask extends Task<Foo> { | |
final private Foo fooInput; | |
FooModifierTask(Foo fooInput) { | |
this.fooInput = fooInput; | |
} | |
@Override protected Foo call() throws Exception { | |
Thread.currentThread().sleep(1000); | |
return new Foo(fooInput); | |
} | |
} | |
} | |
class Foo { | |
private final int answer; | |
Foo() { | |
answer = random.nextInt(100); | |
} | |
Foo(Foo input) { | |
answer = input.getAnswer() + 42; | |
} | |
public int getAnswer() { | |
return answer; | |
} | |
} | |
private static final Random random = new Random(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import javafx.application.Application; | |
import javafx.concurrent.*; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.geometry.Pos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.*; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
public class FooServiceApp extends Application { | |
public static void main(String[] args) { Application.launch(args); } | |
@Override public void start(final Stage stage) throws Exception { | |
final FooGenerator fooGenerator = new FooGenerator(); | |
final Label fooDisplay = new Label(); | |
final Button fooMaker = new Button("Make Foo"); | |
fooMaker.setOnAction(new EventHandler<ActionEvent>() { | |
@Override public void handle(ActionEvent t) { | |
fooDisplay.setText("..."); | |
if (fooGenerator.getState() == Worker.State.READY) { | |
fooGenerator.start(); | |
} else { | |
fooGenerator.restart(); | |
} | |
} | |
}); | |
fooMaker.disableProperty().bind(fooGenerator.runningProperty()); | |
fooGenerator.setOnSucceeded(new EventHandler<WorkerStateEvent>() { | |
@Override public void handle(WorkerStateEvent t) { | |
Foo myLovelyNewFoo = fooGenerator.getValue(); | |
fooDisplay.setText(myLovelyNewFoo.getAnswer() + ""); | |
} | |
}); | |
final VBox layout = new VBox(10); | |
layout.setAlignment(Pos.CENTER); | |
layout.getChildren().setAll(fooMaker, fooDisplay); | |
layout.setStyle("-fx-padding: 10px; -fx-background-color: cornsilk;"); | |
Scene scene = new Scene(layout); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
class FooGenerator extends Service<Foo> { | |
@Override protected Task createTask() { | |
return new Task<Foo>() { | |
@Override protected Foo call() throws Exception { | |
Thread.currentThread().sleep(1000); | |
return new Foo(); | |
} | |
}; | |
} | |
} | |
class Foo { | |
private int answer = random.nextInt(100); | |
public int getAnswer() { | |
return answer; | |
} | |
} | |
private static final Random random = new Random(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution to the StackOverflow question:
How to communicate user defined objects and exceptions between Service and UI in JavaFX2?