今天模擬檔案copy的進度,狀態的顯示通常是把label bind到Worker裡的property,這樣即可同步顯示狀態,包括以讀取多少byte、目前完成百分比等,還挺有趣的,看看明天還能摸出什麼功能,希望鐵人賽結束的時候可以做出工作上用得的UI。
Main部分沒有變,主要變化的是:
Data
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myfxthread;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import javafx.concurrent.Task;
import javafx.concurrent.Worker;
/**
*
* @author joombuopre
*/
public class Data {
public Worker<String> worker;
Path seo=Paths.get("E:\\jdk-7u7-windows-i586.exe");
Path des=Paths.get("D:\\jdk-7u7-windows-i586.exe");
public Data() throws IOException{
final BasicFileAttributes att= Files.readAttributes(seo, BasicFileAttributes.class);
final InputStream in = Files.newInputStream(seo, StandardOpenOption.READ);
worker = new Task<String>(){
@Override
protected String call() throws Exception {
final long total=att.size();
byte[] buffer = new byte[10000];
updateProgress(0, total);
int bytereaded=0;
long i=0;
while((bytereaded =in.read(buffer)) != -1){
i=i+bytereaded;
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
updateProgress(i, total);
}
return null;
}
};
}
}
Elements(layout部分)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myfxthread;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderPaneBuilder;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
/**
*
* @author joombuopre
*/
public class Elements {
public Button sb=new Button("Start");
public Button cb=new Button("Cancel");
public Scene scene;
public ProgressBar pb=new ProgressBar();
public Label l1=new Label("File Copy");
public Label ws=new Label();
public Label pgp= new Label();
public Label wd= new Label();
public HBox hBox;
public BorderPane bl;
public VBox vBox;
public Elements(Data data){
pb.setMinWidth(200);
pb.progressProperty().bind(data.worker.progressProperty());
ws.textProperty().bind(Bindings.format("%s", data.worker.stateProperty()));
wd.textProperty().bind(data.worker.workDoneProperty().asString());
pgp.textProperty().bind(Bindings.format("%5.2f%%", data.worker.progressProperty().multiply(100)));
vBox=VBoxBuilder.create()
.padding(new Insets(10,10,10,10))
.spacing(10)
.alignment(Pos.CENTER)
.children(ws, wd,pgp)
.build();
hBox=HBoxBuilder.create()
.padding(new Insets(10,10,10,10))
.spacing(10)
.alignment(Pos.CENTER)
.children(sb, cb)
.build();
scene= SceneBuilder.create()
.root(BorderPaneBuilder.create()
.padding(new Insets(10,10,10,10))
.top(pb)
.center(vBox)
.bottom(hBox)
.build())
.build();
}
}
開始Run==>
結束==>
Note:開發javaFX現在來說還是Netbeans比較好用一點點