1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class PreparingTestWindow
{
/*
* The scene Title
*/
TextField title = new TextField("Preparing Test Operation");
/*
* A layout to host the Text field and the button
*/
HBox hboxLayout = new HBox(5);
/*
* The layout of the page
*/
VBox layout = new VBox(10);
/*
* The stage of the page
*/
Stage stage;
ProgressBar pb = new ProgressBar();
Task copyWorker;
/**
* The Default Constructor : builds the user interface of the page
*/
public PreparingTestWindow(Stage stage)
{
this.stage = stage;
/*
* Set the style of the text title
*/
title.setStyle("-fx-text-fill: blue;");
/*
* Progress Bar to show the preparation of the test
*/
/*
* set the text field and the button in the same line (HBox)
*/
hboxLayout.getChildren().addAll(pb);
/*
* Set all UI components in the final page layout
*/
layout.getChildren().addAll(title, hboxLayout);
layout.setPrefWidth(400);
layout.setPrefHeight(100);
layout.setPadding(new Insets(10, 10, 10, 10));
stage.setScene(new Scene(layout));
stage.setTitle("Chose directory");
}
@SuppressWarnings("rawtypes")
public Task createWorker()
{
return new Task()
{
@Override
protected Object call() throws Exception
{
for (int i = 0; i < 10; i++)
{
Thread.sleep(2000);
updateMessage("2000 milliseconds");
updateProgress(i + 1, 10);
}
return true;
}
};
}
public void open()
{
stage.show();
}
public void close()
{
stage.close();
}
public void start()
{
title.setDisable(true);
pb.setProgress(0);
copyWorker = createWorker();
pb.progressProperty().unbind();
pb.progressProperty().bind(copyWorker.progressProperty());
copyWorker.messageProperty().addListener(new ChangeListener<String>()
{
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue)
{
System.out.println(newValue);
}
});
new Thread(copyWorker).start();
}
public void stopp()
{
title.setDisable(false);
copyWorker.cancel(true);
pb.progressProperty().unbind();
pb.setProgress(0);
title.setText("cancelled.");
}
} |