繁体   English   中英

如何关闭java fx中的弹出框

[英]How to close pop up box in java fx

我正在用java fx构建一个应用程序,并且在从FTP服务器下载文件时显示进度条,我试图关闭下载后的弹出条。 我的代码是

private void download() {
    FTPClient ftpClient = new FTPConnection().makeConnection(loc);

    try {
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        success = ftpClient.changeWorkingDirectory(PATH + preset + "/" + file_to_download + offset);
        System.out.println("Download Path:-" + PATH + preset + "/" + file_to_download + offset);
        if (!success) {
            System.out.println("Could not changed the directory to RIBS");
            return;
        } else {
            System.out.println("Directory changed to RIBS");
        }
        FTPFile[] files = ftpClient.listFiles();
        for (FTPFile file : files) {
            if (file.getName().contains(".zip")) {
                dfile = file;
            }

        }
        fsize = dfile.getSize();
        fileMap.put("build", dfile.getName());
        //primaryStage = (Stage) ap.getScene().getWindow();

        String homePath = System.getProperty("user.home");
        File downloadPath = new File(homePath + "\\downloadfolder\\" + osVer);
        if (!downloadPath.exists()) {
            if (downloadPath.mkdirs()) {
                System.out.println("Directory is created!");
            } else {
                System.out.println("Failed to create directory!");
            }
        }
        // System.out.println(chosenDir.getAbsolutePath());
        filePath = new File(downloadPath + "/" + dfile.getName());
        if (filePath.exists()) {
            System.out.println("File altready exist");
            JOptionPane.showMessageDialog(null, "File already exists", "InfoBox: " + "",
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        } else {
            fileMap.put("path", filePath.toString());
            fileMap.put("kind", "RIBS");
            downloadFile = new File(downloadPath + "/" + dfile.getName());
            // Progress bar
            Task<Void> progress = new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    try {
                        for (long progress = 0; progress < dfile.getSize() ; progress = downloadFile.length()) {
                            Thread.sleep(300);
                            System.out.println(progress);
                            updateProgress(progress, dfile.getSize());

                        }
                    } catch (InterruptedException e) {
                    }
                    finally {

                    }
                    return null;
                }
            };
            ProgressBar slider = startProgressBar();
            slider.progressProperty().bind(progress.progressProperty());

            // download task
            Task downloadTask = new Task<Void>() {
                @Override
                public Void call() throws IOException {
                    try {
                        long len = dfile.getSize();
                        System.out.println("File From Server:::::: " + len);

                        System.out.println("DOWNLOAD FILE:::::" + downloadFile);
                        outputFile = new FileOutputStream(downloadFile);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    ftpClient.sendNoOp();
                    ftpClient.setConnectTimeout(1000);
                    // ftpClient.retrieveFile(dfile, output);
                    if (ftpClient.retrieveFile(dfile.getName(), outputFile) == true) {
                        System.out.println("ReplyCOde:-" + ftpClient.getReplyCode());
                        downloadButton.setDisable(true);

                        try {
                            String homePath = System.getProperty("user.home");
                            Desktop.getDesktop().open(new File(homePath + "/downloadfolder"));
                            //primaryStage.hide();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        System.out.println("LOCAL FILE LENGTH:-" + downloadFile.length());
                        if (outputFile != null) {
                            try {
                                outputFile.close();
                                ftpClient.logout();
                                ftpClient.disconnect();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                    return null;
                }
            };
            Thread t = new Thread(downloadTask);
            t.start();

            Thread thread = new Thread(progress);
            thread.start();

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        //primaryStage.hide();
    }
    return;
}

我尝试了一些关闭它的方法,但无法完成。

String homePath = System.getProperty("user.home");
                                Desktop.getDesktop().open(new File(homePath + "/LightroomBuildsApp"));
                                primaryStage.hide();

但抛出java.lang.IllegalStateException。 我也通过设置条件在进度任务中尝试了它

if (ftpClient.retrieveFile(dfile.getName(), outputFile) == true) {
primaryStage.hide();
}

但它似乎也不起作用。

下载文件的大小略小于服务器中显示的文件大小,因此我也无法进行比较。

您可以添加一个成功完成任务后调用的EventHandler

yourTask.setOnSucceeded(evt -> primarystage.hide());

Java文档

暂无
暂无

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

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