0

Im trying to build an Installer/Updater for a project im working on. My only problem im facing is that my variable of my progess bar doesn't want to be displayed in a label :C. I already looked up and found an answer from Sebastian who said myLabel.textProperty().bind(valueProperty); should work but ... well you guess the outcome. Eclipse says I have to change the type of my int to: ObservableValue<? extends String> and when I changed it it says I have to change it back to int. I dont know what I have to do now ://

EDIT: Full code of my controller class

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) {
    }   
};

}

  • And where is the label you are talking about? – hotzst Jan 04 '16 at 17:52
  • @hotzst I edited my Post. – Alexander Knorr Jan 04 '16 at 17:56
  • 1
    You need to use [Platform.runLater](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Platform.html#runLater-java.lang.Runnable-) for the code in your download routine which is currently running off the JavaFX application thread yet manipulating the active scene graph (a serious error which may cause unpredictable results). Also, binding within a loop in the manner you are doing is not something you want to do. You should also research [Concurrency in JavaFX](https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm) and probably use a Task. – jewelsea Jan 04 '16 at 20:32
  • @jewelsea Thanks. I'm looking into it. – Alexander Knorr Jan 05 '16 at 11:35
  • If the application being installed is a Java application, you should be using Web Start, which has its own installer (which even contains a progress bar like the one you're trying to make). Web Start comes with every Java installation. – VGR Jan 05 '16 at 23:40

1 Answers1

0

Create a DoubleProperty that is bound to the label and is updated when you update the progressbar.

DoubleProperty progress = new SimpleDoubleProperty(0);
progText.textProperty().bind(progress.asString());

...
// update progress bar
pb.setProgress(cp*0.0001);
progress.setValue(cp*0.0001)
hotzst
  • 7,238
  • 9
  • 41
  • 64
  • Im having an exception in Thread 4. The error it in line 69 which is the Line where I bind the progress as a String to my progText Label. Seems like I have to create another Thread? Code works in another class though, – Alexander Knorr Jan 04 '16 at 18:12
  • 1
    @AlexanderKnorr That sounds like a completely different issue, so if you can't solve it by searching on this site (or by other research) you should post a new question. You may want to read up some of this site's [help] pages, particularly ["how to ask"](http://stackoverflow.com/help/how-to-ask). You might also benefit from [this question](http://stackoverflow.com/questions/3988788/) on reading stack traces. – James_D Jan 04 '16 at 19:01