Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: how to temporary block the GUI

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

Independently on which screen the user is seeing, if the application loses the connection to the server I'd like to block everything (the users cannot open any new windows or click anywhere) until the application is connected again (it doesn't matter if that needs 5 minutes or 5 hours). Nevertheless, on the top of everything should appear an alert message (always on the top).

The java class which is listening to the server connection doesn't have any reference to JavaFX containers. That's 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
}

Which 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 showing something on the top (even a simple text message) and, in the meanwhile, make the application in background more opaque.

like image 463
db92 Avatar asked Nov 19 '22 01:11

db92


1 Answers

You need a modal Dialog. Create such a dialog and show it when your connection goes down. Then use a Thread which periodically checks if your connection is back up. The time the connection comes alive kill the dialog. Since the dialog is modal it means that you can do nothing to the UI until it is resolved. See this.

like image 117
gvlachakis Avatar answered Dec 22 '22 00:12

gvlachakis