【发布时间】:2016-09-07 02:19:42
【问题描述】:
参考我的这个问题fxml file not behaving as expected in OS X,我能够解决第一个问题,但我仍然面临进度条的问题。当我的应用程序窗口未最大化但当我的应用程序窗口最大化时我的进度条对话框占据整个屏幕时,它的行为很好。我的进度条代码是这样的:
public ProgressBar startProgressBar() {
primaryStage = new Stage();
ProgressBar pb = new ProgressBar(0);
//ProgressIndicator pi = new ProgressIndicator(0);
//pi.progressProperty().bind(pb.progressProperty());
HBox hb = new HBox();
hb.setSpacing(5);
hb.setAlignment(Pos.CENTER);
hb.getChildren().addAll(pb);
Scene scene = new Scene(hb, 300, 100);
primaryStage.setScene(scene);
primaryStage.setTitle("Downloading Build...");
primaryStage.show();
return pb;
}
我尝试过使用以下方法,但这些不起作用。
primaryStage.setFullScreen(false);
primaryStage.setMaxHeight(200);
primaryStage.setMaxWidth(400);
但这也行不通。如果我将高度和宽度固定为恒定,则对话框仅采用该高度和宽度,但背景屏幕显示为黑色。
一旦用户单击下载按钮,我就会触发进度条,这是我的下载过程代码。在进度条中,我正在比较我的下载文件大小和来自服务器的文件大小,我正在用本地目录中的文件大小更新进度条。
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;
}
}
fileMap.put("build", dfile.getName());
//primaryStage = (Stage) ap.getScene().getWindow();
String homePath = System.getProperty("user.home");
File downloadPath;
if(getOS().equals("Mac"))
{
downloadPath = new File(homePath + "/file path/" + osVer);
}
else
{
downloadPath = new File(homePath + "\\file path\\" + 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 {
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 + "/file path"));
//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();
downloadTask.setOnSucceeded(evt->primaryStage.hide());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//primaryStage.hide();
}
return;
}
任何对话框都具有与 Mac OS X 类似的行为。我尝试了 JavaFx 警报框,它也具有相同的行为。有什么解决办法吗?
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog");
alert.setHeaderText(null);
alert.setContentText("cannot connec to server or file not present at server");
【问题讨论】:
-
minimal reproducible example 将允许 Mac 用户测试您的方法。
-
我已经更新了我的问题。
标签: java macos javafx javafx-2