如何在java fx中关闭弹出框

时间:2016-05-05 16:06:09

标签: java javafx javafx-2

我正在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();
}

但它似乎也不起作用。

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

1 个答案:

答案 0 :(得分:0)

您可以在任务成功完成后添加被调用的EventHandler

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

javadoc