Runnable
을 구현하는 다른 클래스의 Platform.runLater
을 사용하여 TextArea를 업데이트하려고합니다. 클래스에 내 모든 GUI가 있습니다 (내 TextArea가있는 곳입니다), GUI를 만들 때 new server
스레드를 만들고 실행하고 있습니다. 내 TextArea
을 업데이트하기 위해 Server
스레드의 Platform.runLater
을 사용하려고 시도했지만 Platform.runLater
캔트가 내 TextArea에 도달하지 못했습니다. Modifying JavaFX gui from different thread in different classJavaFX가 Platform.runLater를 사용하는 다른 스레드에서 TextArea를 업데이트합니다.
을하지만 자바는 늘 서버 생성자에 대한 매개 변수로 내 SimulationWindow 인스턴스를 전달하자 : 그들이 여기처럼
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의 인스턴스를 통과했습니다. 다른 솔루션은 Server 및 SimulationWindow 클래스를 하나의 클래스로 보유하지만이를 분리하여 유지하려고합니다. 팁을 주시면 감사하겠습니다!
서버에서 textArea를 매개 변수로 사용하는 생성자를 추가하십시오. – kleopatra
* "하지만 Java는 서버 구성 자의 매개 변수로 Java SimulationWindow 인스턴스를 전달하지 않습니다."* What' SimulationWindow' 인스턴스 : 코드에 인스턴스가 없습니다. 'displaySimulationWindow()'는 실제로'static' 일 필요가 있습니까? –
안녕하세요 @ James_D 질문에 인스턴스 메서드를 추가했습니다. 그 점을 지적 해 주셔서 감사합니다. 정적 및 이제 서버 생성자에 인스턴스를 보낼 수 있지만 여전히 서버 내에서 serverTextArea 액세스 할 수 없습니다. – CCBoy