Runnable
を実装する別のクラスからPlatform.runLater
を使用してTextAreaを更新しようとしています。 私はすべてのGUIをクラスに持っています(私のTextAreaがあるところです)、私はnew server
スレッドを作成していて、GUIを作成するときにそれを実行しています。私のTextArea
を更新するためにServer
スレッドからPlatform.runLater
を使用しようとしていますが、Platform.runLater
は私のTextAreaに届きません。JavaFX update TextAreaをPlatform.runLaterを使用して別のスレッドから
public class SimulationWindow {
public SimulationWindow instance() {
return this;
}
public static void DisplaySimulationWindow() throws FileNotFoundException {
Stage SimuStage = new Stage();
SimuStage.initModality(Modality.APPLICATION_MODAL);
SimuStage.setTitle("Simulation Window");
Server myServer = new Server(instance());
Thread serverThread = new Thread(myServer);
serverThread.start();
TextArea serverTextArea;
.
.
.
}
public class Server implements Runnable {
@Override
public void run() {
while(true){
whileConnected();
.
.
}
private void whileConnected() throws IOException {
sendMessage(message);
do {
try {
message = (String) input.readObject();
showMessage(message);
.
.
.
}
private void showMessage(String x) {
Platform.runLater(() -> serverTextArea.appendText(x));
}
私は、彼らがここで行うようにサーバーのコンストラクタに私のSimulationWindowのインスタンスを渡してみました:Modifying JavaFX gui from different thread in different class
しかし、Javaの文句を言わないサーバーのコンストラクタのパラメータとしての私のSimulationWindowインスタンスを渡すことができます。 他のソリューションでは、ServerとSimulationWindowのクラスを保持していますが、それらを分離しておく必要があります。 ヒントありがとうございます!
、*パラメータ – kleopatra
ようにtextAreaを取るコンストラクタを追加し、「しかし、Javaは文句を言わないサーバーのコンストラクタのパラメータとしての私のSimulationWindowインスタンスを渡しましょう。」*何 'SimulationWindow'インスタンス:あなたのコード内の1つのがありません。 'displaySimulationWindow()'は本当に 'static'である必要がありますか? –
こんにちは@James_D私は質問にインスタンスメソッドを追加しました。それを指摘してくれてありがとう。私は静的を削除し、今私はサーバーのコンストラクタにインスタンスを送信することができますが、私はサーバー内からserverTextAreaにアクセスできません。 – CCBoy