简体   繁体   English

JavaFX Stage显示空的场景

[英]JavaFX Stage shows empty scene

I've been trying to open a new Window in order to display a progress bar, from a controller : 我一直试图从控制器打开一个新窗口以显示进度条:

    Stage fenetre = new Stage();
    fenetre.initModality(Modality.APPLICATION_MODAL);

    FXMLLoader loader;
    Parent root;
    Scene chargementBox;

    loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("/views/Chargement.fxml"));
    loader.load();
    root = loader.getRoot();
    chargementBox = new Scene(root);

    fenetre.setTitle("Chargement");
    fenetre.resizableProperty().set(false);    
    fenetre.setScene(chargementBox);
    fenetre.show();

It shows the window. 它显示窗口。 But it's empty : 但它是空的:

This is what it should show 这是应该显示的

This is what i got instead 这是我得到的

I tried everything, I used other FXML files, the window sizes are correct but it's always empty. 我尝试了一切,使用了其他FXML文件,窗口大小正确,但是始终为空。 The same code works on other Controllers, but not here. 相同的代码可在其他Controller上使用,但不适用于此处。

Help please. 请帮助。 There is no exceptions and no errors. 没有例外,没有错误。 Thank's 谢谢


Edit : I've found the reason why it doesn't show, it's because later in the code i have this function : Transport.send(message); 编辑:我发现了它不显示的原因,这是因为稍后在代码中我具有以下功能: Transport.send(message); that blocks the program from refreshing my scene and displaying the elements. 阻止程序刷新我的场景并显示元素。 Is there a way i can run that line in the background or in another thread (I never used threads before.) Thank's again for the help. 有没有一种方法可以在后台或其他线程中运行该行(我以前从未使用过线程。)再次感谢您的帮助。

To run something on the background thread you need to either use a task (useable once) or a service (reusable). 要在后台线程上运行某些内容,您需要使用任务(一次可用)或服务(可重复使用)。

This is how you can use a service: 这是使用服务的方式:

Service<Void> service = new Service<Void>(){
            @Override
            protected Task<Void> createTask() {
                return new Task<Void>() {
                    @Override
                    protected Void call() throws Exception {
                            //do your logic here.
                        return null;
                    }
                };
            }
        };
        service.setOnSucceeded(event -> {
                //do some processing when complete
             });
        service.setOnFailed(event -> {
               //do some processing when it failes
             });

        service.start();

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

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