简体   繁体   English

JavaFX:如何临时阻止GUI

[英]JavaFX: how to temporary block the GUI

I'm trying to figure out if blocking the GUI is possible. 我想弄清楚是否可以阻止GUI。 Basically, my application (which is using the NetBeans Platform and JavaFX ) has a connection to the server. 基本上,我的应用程序(使用NetBeans平台JavaFX )与服务器有连接。

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). 无论用户看到哪个屏幕,如果应用程序丢失了与服务器的连接,我想阻止所有内容(用户无法打开任何新窗口或点击任何地方),直到应用程序再次连接为止(无论是否重要)需要5分钟或5个小时)。 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. 正在侦听服务器连接的java类没有对JavaFX容器的任何引用。 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. 这个想法是在顶部显示一些东西(甚至是简单的文本消息),同时使应用程序在后台更加不透明。

You need a modal Dialog . 你需要一个模态 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. 然后使用一个Thread定期检查你的连接是否已备份。 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. 由于对话框是模态的,因此意味着在解析之前,您无法对UI执行任何操作。 See this . 看到这个

Use Alert or Dialog components. 使用AlertDialog组件。 You can style them by CSS or add custom content. 您可以通过CSS设置样式或添加自定义内容。 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 your entire Scene : 您还可以在整个Scene之上添加其他Pane

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM