ProgressBar没有使用javafx

时间:2016-04-01 09:34:20

标签: java javafx javafx-2 javafx-8

我正在使用javafx构建桌面应用。 我的fxml文件是: -

use warnings;
use strict;
open my $fh1, "<", "f1.txt" or die"$!";
my @ar = <$fh1>;
my $exculed = join("|",@ar);
$exculed=~ s/\n|\s//g;   #your input have some spaces so i used substitution instead of chomp

open my $fh2, "<", "f2.txt";
open my $nw, ">", "newfile.txt";
my @br = <$fh2>;
@br = grep{!/$exculed/g} @br if ($exculed ne "");
print $nw @br;

我的下载按钮控制器类是: -

<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.Buildsapp.Main.BuildsController" fx:id="ap">
<children>
  <HBox alignment="CENTER_LEFT" layoutX="6.0" layoutY="14.0" spacing="20.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
     <children>
          <ComboBox fx:id="versionCombo" prefHeight="31.0" prefWidth="108.0" promptText="7.0 Win">
            <items>
          <FXCollections fx:factory="observableArrayList">
            <String fx:value="Win" />
            <String fx:value="Mac" />
            </FXCollections>
        </items>
              <value>
          <String fx:value="Win" />
      </value>
          </ComboBox>
        <ComboBox fx:id="versionNo" prefHeight="31.0" prefWidth="89.0" promptText="7.0" />
          <Button fx:id="downloadButton" minWidth="80.0" mnemonicParsing="false" text="Download" />
          <Button fx:id="installButton" minWidth="80.0" mnemonicParsing="false" text="Install" />
        <ComboBox fx:id="locCombo" prefHeight="31.0" prefWidth="103.0">
        <items>
          <FXCollections fx:factory="observableArrayList">
            <String fx:value="CJ" />
            <String fx:value="MN" />
            </FXCollections>
        </items>
        <value>
          <String fx:value="CJ" />
      </value>
        </ComboBox>
            <ProgressBar fx:id="progressBar1" prefHeight="23.0" prefWidth="102.0" progress="0.0" />
     </children>
     <padding>
        <Insets left="10.0" right="3.0" top="5.0" />
     </padding>
  </HBox>
    <TableView fx:id="tableView" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="2.0" AnchorPane.rightAnchor="2.0" AnchorPane.topAnchor="40.0">
        <columns>
            <TableColumn fx:id="builds" prefWidth="482.0" text="Builds" />
            <TableColumn fx:id="date" minWidth="0.0" prefWidth="124.0" text="Date" />
            <TableColumn fx:id="release" prefWidth="168.0" text="Release" />
        </columns>
    </TableView>

 </children>
</AnchorPane>

下载方法如下。

downloadButton.setOnMouseClicked(new EventHandler<MouseEvent>() {

    @Override
    public void handle(MouseEvent event) {
        download();
    }

});

我的进度条形码是: -

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.getName();
            }

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

        String homePath = System.getProperty("user.home");
        File downloadPath = new File(homePath + "\\LightroomBuildss\\" + 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);
        if (filePath.exists()) {
            System.out.println("File altready exist");
            return;
        } else {
            fileMap.put("path", filePath.toString());
            fileMap.put("kind", "RIBS");
            Task task = new Task<Void>() {
                @Override
                public Void call() throws IOException {
                    try {
                        output = new FileOutputStream(downloadPath + "/" + dfile);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    ftpClient.sendNoOp();
                    ftpClient.setConnectTimeout(1000);

                    Task<Void> sleeper = new Task<Void>() {
                        @Override
                        protected Void call() throws Exception {
                            try {
                                for (double progress = 0.0; progress < 100.0; progress++) {
                                    Thread.sleep(100);
                                    updateProgress(progress, 100);
                                    System.out.println(progress);
                                }
                            } catch (InterruptedException e) {
                            }
                            return null;
                        }
                    };
                    ProgressBar slider = startProgressBar();
                    slider.progressProperty().bind(sleeper.progressProperty());
                    Thread thread = new Thread(sleeper);
                    thread.start();
                    if (ftpClient.retrieveFile(dfile, output) == true) {
                        downloadButton.setDisable(true);

                        outsize = dfile.length();
                    }
                    return null;
                }
            };
            Thread t = new Thread(task);
            t.start();

        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

我在下载开始之前从下载方法调用startProgessBar方法,但在调用该方法之后,进度条没有启动,并且在方法调用之后写入的代码(在这种情况下禁用下载按钮)也没有虽然在所需的文件夹中下载了版本,但仍然会执行。如果startProgressBar方法在代码工作正常后没有被调用。

2 个答案:

答案 0 :(得分:0)

检查这些。

primaryStage=//this is global
primaryStage= (Stage) ap.getScene().getWindow();//why do this?
//i guess its simply primaryStage = primaryStage

Scene scene = new Scene(root);//scene already has a root
scene.setRoot(hb);//why do this?
//if you do not need the Group, why set it to your scene

(Stage) ap.getScene().getWindow();//this a.k.a primaryStage is already shown
primaryStage.show();//this isn't needed as your last line.

以上这些并不能解决您的问题,但这可以借助上述

来解决
startProggressBar();
if (ftpClient.retrieveFile(dfile, output) == true) {
       downloadButton.setDisable(true);//this guy is an orphan.
       outsize = dfile.length();
 }
//when startProggressBar(); is done your primaryStage's Scene is changed

希望它有用

答案 1 :(得分:0)

有很多错误。首先,不要在任务中放置进度指示器的窗口设置。设置它,然后定义任务并运行它。

我看不到任务进度和屏幕之间的任何关联。

根据它的valueProperty修改滑块似乎不会让它在屏幕上更新。最好通过绑定直接链接到ProgressBar。

这里有一些简单的代码可以完成你正在寻找的东西,在任务中使用虚拟操作而不是下载:

public class SampleTask extends Application {

private BorderPane testPane;

class TestPane extends BorderPane {

    public TestPane() {
        Button button = new Button("Click Here");
        setCenter(button);
        button.setOnAction(evt -> {
            Task<Void> sleeper = new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    try {
                        for (double progress = 0.0; progress < 100.0; progress++) {
                            Thread.sleep(100);
                            updateProgress(progress, 100);
                            System.out.println(progress);
                        }
                    } catch (InterruptedException e) {
                    }
                    return null;
                }
            };
            ProgressBar slider = startProgressBar();
            slider.progressProperty().bind(sleeper.progressProperty());
            Thread thread = new Thread(sleeper);
            thread.start();
        });
    }
}

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Task Progress Tester");
    testPane = new TestPane();
    primaryStage.setScene(new Scene(testPane, 300, 250));
    primaryStage.show();
}

public ProgressBar startProgressBar() {
    Stage 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, pi);
    Scene scene = new Scene(hb, 300, 100);
    primaryStage.setScene(scene);
    primaryStage.show();
    return pb;
}

}