バックグラウンドスレッドでタスクを実行する方法を学ぶ前に、インターフェイスを使用してコントローラからUIにメッセージを投稿する方法を学びました。インターフェイスを使用すると、どのコントローラでもシステムメッセージを表示できます。タスクからupdateMessageにバインドする方法を理解することはできません。JavaFX並行性:インタフェースを持つコントローラへのメッセージ()の更新方法は?
マイインタフェース:
public interface SystemMessage {
void postMessage(String outText);
}
マイコントローラ:
public class MainController implements SystemMessage {
@FXML
public DialogPane systemMessage;
@Override
public void postMessage(String outText) {
systemMessage.setContentText(outText);
}
//***I have tried the following to bind the dialog pane to the updateMessage
systemMessage.contentTextProperty().bind(task.messageProperty());
//***InteliJ tells me that it can not resolve contetTextProperty or task.
}
新しいスレッド上のタスクと私のメインクラス。
private SystemMessage mainController;
FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml/entry.fxml"));
Parent root = loader.load();
this.mainController = (SystemMessage) loader.getController();
api = new Api();
machineId = new Identity();
db = new SqLite();
//mainController.postMessage(task.messageProperty()).bind();
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
Task<String> task = new Task<String>() {
@Override protected String call() throws Exception {
checkAsiServer();
updateMessage("API Checked.");
checkMachineId();
updateMessage("Machine Id Checked.");
checkDb();
updateMessage("Server Checked.");
return "done";
}
};
私はでcotrollerの私DialogPaneをバインドしようとしている:
systemMessage.contentTextProperty().bind(task.messageProperty());
しかし、それはcontentTextPropertyを解決できないと私はどちらかのタスクオブジェクトについてのコントローラに伝える方法がわかりません。
最後のコードブロックはどこから来たのですか?これはコントローラ内部にありますか?もしそうなら、問題の内容はわかりません。 –
@James_Dいいえ最後のコードブロックは、タスクが実行されているMainにあります。あなたは私がタスクコンポーネントを理解するのを手伝った! Incedentally私はRunLater()でこの作業を行うことができますが、私は私のインターフェイスへのupdateMessageはもっと....エレガントだろうと思っていますか? – silversunhunter
@ James_D ..コントローラにあるsystemMessage ...コードブロックを参照していた場合。 contentTextProprtyまたはtaskは解決できません。 – silversunhunter