Skip to content

Instantly share code, notes, and snippets.

@james-d
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james-d/346c63b6e4db4d94914f to your computer and use it in GitHub Desktop.
Save james-d/346c63b6e4db4d94914f to your computer and use it in GitHub Desktop.
Simple example showing a popup shown from a background thread (simulating receiving messages from a server...). (No fancy UI, just showing the idea).
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
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.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.stage.Window;
public class PopupFromBackgroundThreadDemo extends Application {
private final Executor exec = Executors.newCachedThreadPool(runnable ->
new Thread(runnable){{ setDaemon(true);}});
private final Logger logger = Logger.getLogger(getClass().getName());
@Override
public void start(Stage primaryStage) {
Popup popup = new Popup();
Runnable messageReadSimulator = () -> {
Random rng = new Random();
while (true) {
try {
Thread.sleep(rng.nextInt(5000)+5000);
} catch (InterruptedException exc) {
logger.log(Level.WARNING, "Interruption to background thread", exc);
break ;
}
showMessage("The server sent a message", popup, primaryStage);
}
};
exec.execute(messageReadSimulator);
TextField textField = new TextField();
Button button = new Button("Show message");
EventHandler<ActionEvent> handler = e -> {
showMessage(textField.getText(), popup, primaryStage);
textField.setText("");
};
textField.setOnAction(handler);
button.setOnAction(handler);
VBox root = new VBox(10, textField, button);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 250, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
public void showMessage(String message, Popup popup, Window owner) {
logger.info("showMessage(...) called from thread "+Thread.currentThread()+". "
+ "FX Application Thread? "+Platform.isFxApplicationThread());
if (Platform.isFxApplicationThread()) {
_showMessage(message, popup, owner);
} else {
Platform.runLater(() -> _showMessage(message, popup, owner));
}
}
private void _showMessage(String message, Popup popup, Window owner) {
logger.info("_showMessage(...) called from thread "+Thread.currentThread()+". "
+ "FX Application Thread? "+Platform.isFxApplicationThread());
if (! Platform.isFxApplicationThread()) {
throw new Error("Not on FX Application Thread");
}
popup.getContent().clear();
Label label = new Label(message);
Button button = new Button("OK");
button.setOnAction(e -> popup.hide());
VBox vbox = new VBox(10, label, button);
vbox.setStyle("-fx-background-color: antiquewhite; -fx-padding: 10;");
vbox.setAlignment(Pos.CENTER);
popup.getContent().add(vbox);
popup.show(owner, owner.getX(), owner.getY()+owner.getHeight());
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment