I noticed that cancelling the following application causes errors since a bound value cannot be set. I tried to add some conditions to prevent this error from happening but to no avail.
The following is the main culprit:
pb.progressProperty().bind(serv.progressProperty());
You can also notice that I tried to use progressProperty and ChangeListener instead of bind method. Unfortunately unsuccessfully. Can I make progressbar functioning without bind method (code above)?
Complete error message when cancel button is pressed is the following:
Service status: SCHEDULED
Let the download begin!
Service status: CANCELLED
Service status cancelled when progress was: 0.09582292038557658
java.lang.RuntimeException: A bound value cannot be set.
at javafx.beans.property.DoublePropertyBase.set(Unknown Source)
at javafx.scene.control.ProgressIndicator.setProgress(Unknown Source)
at dm.Thr$2.handle(Thr.java:73)
at dm.Thr$2.handle(Thr.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
Cancelling!
Finished/cancelled!
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.SkinBase$5.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.SkinBase$5.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1300(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Application:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javafx.application.Application;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Thr extends Application {
@Override
public void start(Stage stage) throws Exception {
final Group rootGroup = new Group();
final Scene scene = new Scene(rootGroup, 500, 400, Color.GHOSTWHITE);
stage.setScene(scene);
stage.setTitle("Testing downloads from Spring");
stage.show();
setS(rootGroup);
}
final VBox vbox = new VBox();
final javafx.scene.control.Button btn = new javafx.scene.control.Button("START!");
final TextArea tf = new TextArea();
final javafx.scene.control.Button btnC = new javafx.scene.control.Button("CANCEL!");
final ProgressBar pb = new ProgressBar();
final FirstLineService serv = new FirstLineService();
boolean cancelled = false;
private void setS(Group rg) {
tf.setPrefRowCount(5);
vbox.getChildren().add(btn);
vbox.getChildren().add(tf);
vbox.getChildren().add(btnC);
vbox.getChildren().add(pb);
pb.setPrefWidth(450);
rg.getChildren().add(vbox);
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
serv.start();
System.out.println("Service status: "+serv.getState());
tf.setText("STARTED!");
}
});
btnC.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
serv.cancel();
System.out.println("Service status: "+serv.getState());
System.out.println("Service status cancelled when progress was: "+serv.getProgress());
tf.appendText("\nCANCELLED!");
pb.setProgress(serv.getProgress());
}
});
//pb.setProgress(0);
if (cancelled!=true) {
pb.progressProperty().bind(serv.progressProperty());
} else {
pb.progressProperty().unbind();
}
/*pb.progressProperty().addListener(new ChangeListener<Task>() {
@Override
public void changed(ObservableValue<? extends Task> arg0,
Task arg1, Task arg2) {
// TODO Auto-generated method stub
pb.setProgress(serv.getProgress());
}
});*/
}
private class FirstLineService extends Service {
protected Task createTask() {
return new Task<Void>() {
protected Void call() {
URL url;
try {
url = new URL("http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.1.1.RELEASE.zip");
InputStream in = url.openStream();
int fileSize = url.openConnection().getContentLength()/1024;
File f = new File("spring.zip");
BufferedInputStream bin = new BufferedInputStream(in, 1024);
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
int n=0;
int k=0;
System.out.println("Let the download begin!");
while(n !=-1){
if (isCancelled()) {
cancelled = true;
n=-1;
System.out.println("Cancelling!");
} else {
k=k+1;
n=bin.read();
bos.write(n);
updateProgress(k/1024,fileSize);
//pb.progressProperty().bind(this.progressProperty());
//pb.setProgress(getProgress());
}
}
bos.close();
System.out.println("Finished/cancelled!");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
};
}
}
public static void main(final String[] arguments)
{
Application.launch(arguments);
}
}