UI のアプリケーション スレッドで長時間実行していますが、これは実行すべきではありません。そうしないと、UI が応答しなくなります。
むしろ、アプリケーション スレッドで実行時間の長いプロセスを作成Task
または実行します。Thread
JavaFX での同時実行の詳細については、このリンクを参照してください。
の短い例を次に示しTask
ます。
import javafx.concurrent.Task;
....
FileChooser fc = new FileChooser();
fc.setTitle("Pointel File");
File file1 = fc.showOpenDialog(MainFrame.objComponent.getPrimaryStage());
final Task task = new Task<Void>() {
@Override
protected Void call() throws Exception {
int i = 0;
while (i < 90000) {
System.out.println(i);
i++;
}
return null;
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
また、JavaFX UI コンポーネントを変更する場合は、次のPlatform.runLater(Runnable r)
ようにコードをブロックでラップすることも忘れないでください。
import javafx.concurrent.Task;
....
final Task task = new Task<Void>() {
@Override
protected Void call() throws Exception {
int i = 0;
while (i < 90000) {
System.out.println(i);
i++;
}
Platform.runLater(new Runnable() {//updates ui on application thread
@Override
public void run() {
//put any updates to ui here dont run the long running code in this block or the same will happen as doing a long running task on app thread
}
});
return null;
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();