JavaFX: how to temporarily block a GUI

I'm trying to figure out if blocking the GUI is possible. Basically, my application (using NetBeans Platform and JavaFX ) has a server connection.

No matter what screen the user sees, if the application loses connection with the server, I would like to block everything (users cannot open any new windows or click anywhere) until the application is connected again (this does not mean if it is needed 5 minutes or 5 hours). However, a warning message should appear at the top of everything (always on top).

The java class that listens for the connection to the server has no reference to JavaFX containers. This is what I actually have:

public class StatusConnectionObserver implements ConnectionObserver { private final Led led; private final Label label; public StatusConnectionObserver(Led led, Label label) { this.led = led; this.label = label; } @Override public void setConnected(boolean connected) { if (connected) { Platform.runLater(() -> { led.setLedColor(Color.rgb(59, 249, 53)); label.setText("Connected"); }); } else { Platform.runLater(() -> { led.setLedColor(Color.RED); label.setText("Disconnected"); }); } } } 

and

 public class ConnectionComponent { private Led led; private Label label; private HBox container; private VBox ledContainer; public ConnectionComponent() { initGraphics(); } public Parent getView() { return this.container; } public void initGraphics() { //Here I set up the elements (label and Led) inside the container } 

What is called here:

  @ServiceProvider(service = StatusLineElementProvider.class) public class ConnectionIndicator implements StatusLineElementProvider { @Override public Component getStatusLineElement() { JFXPanel fxPanel = new JFXPanel(); Platform.setImplicitExit(false); new JavaFXUIThread().runOnUiToolkitThread(() -> { Scene scene = new Scene(new ConnectionComponent().getView()); scene.getStylesheets().add(FXTheme.getDefault().getStylesheet()); fxPanel.setScene(scene); }); return fxPanel; } } 

The idea is to show something on top (even a simple text message) and, in the meantime, make the application in the background more opaque.

+5
source share
2 answers

You need a modal Dialog . Create such a dialogue and show it when your connection goes down. Then use Thread, which periodically checks to see if your connection supports it. The time when the connection comes to life destroys the dialogue. Since the dialog is modal, this means that you cannot do anything with the user interface until it is resolved. See this .

0
source

Use Alert or Dialog components. You can style them with CSS or add custom content. Try this simplest solution:

 Alert a = new Alert(Alert.AlertType.ERROR, "Connection error"); public void createAlert() { a.getDialogPane().getButtonTypes().clear(); a.initModality(Modality.APPLICATION_MODAL); //*************** EDIT *************** a.initStyle(StageStyle.UNDECORATED); a.initOwner(label.getScene().getWindow()); //************************************ } @Override public void setConnected(boolean connected) { if (connected) { Platform.runLater(() -> { label.setText("Connected"); a.show(); }); } else { Platform.runLater(() -> { label.setText("Disconnected"); a.close(); }); } } 

You can also add additional Pane on top of the whole Scene :

 StackPane root = new StackPane(); root.getChildren().addAll(applicationContent); Pane p = new Pane(); p.setStyle("-fx-background-color: rgba(31,31,31,0.6);"); //add Pane to root when disconnected //root.getChildren().add(p); Scene scene = new Scene(root, 300, 250); 
0
source

Source: https://habr.com/ru/post/1265541/


All Articles