JavaFX标签设置为Variable问题

时间:2016-01-04 17:48:12

标签: java javafx

我正在尝试为正在进行的项目构建安装程序/更新程序。 我面临的唯一问题是我的progess栏的变量不希望显示在标签中:C。 我已经抬起头来,找到了塞巴斯蒂安的回答 myLabel.textProperty().bind(valueProperty);应该可以工作,但是......你猜测结果。 Eclipse说我必须将我的int类型更改为:ObservableValue<? extends String>,当我更改它时,它说我必须将其更改回int。我不知道我现在要做什么://

编辑:我的控制器类的完整代码

package application;



 import java.io.BufferedOutputStream;
   import java.io.FileNotFoundException;
   import java.io.IOException;
   import java.net.HttpURLConnection;
   import java.net.URL;

   import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;

public class Controller {

@FXML
ProgressBar pb;
Label progText;


public void install(){
    new Thread(new Runnable() {
        @Override public void run() {   
            download();

        }}).start();
};


public void load(){
    new Thread(new Runnable() {
        @Override public void run() {   
            download();
            Unzip.extract();
            System.out.println("Finished");   
        }}).start();
};

public void download(){
    try {
        System.out.println("Start");
        URL url = new URL("https://www.dropbox.com/s/27d4us64oqifuph/modpack.zip?dl=1");
        HttpURLConnection httpConnection = (HttpURLConnection) (url.openConnection());
        long completeFileSize = httpConnection.getContentLength();

        java.io.BufferedInputStream in = new     java.io.BufferedInputStream(httpConnection.getInputStream());
        java.io.FileOutputStream fos = new java.io.FileOutputStream(
                "modpack.zip");
        java.io.BufferedOutputStream bout = new BufferedOutputStream(
                fos, 1024);
        byte[] data = new byte[1024];
        long downloadedFileSize = 0;
        int x = 0;
        while ((x = in.read(data, 0, 1024)) >= 0) {
            downloadedFileSize += x;

            //calculate progress
            int cp = (int) ((((double)downloadedFileSize) / ((double)completeFileSize)) * 10000);

            DoubleProperty progress = new SimpleDoubleProperty(0);



            // update progress bar
            pb.setProgress(cp*0.0001);
            progress.setValue(cp*0.0001);
            progText.textProperty().bind(progress.asString());   
            bout.write(data, 0, x);
        }
        bout.close();
        in.close();
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }   
};

}

1 个答案:

答案 0 :(得分:0)

创建绑定到标签的DoubleProperty,并在更新进度条时更新。

{{1}}