我正在制作一个正在监听输入的套接字(目前只是来自终端)。当我读取输入时,我想调用控制器类中的函数,然后将其发送到我的应用程序GUI。这应该是一项简单的任务,但为了让我的套接字线程与JavaFX线程进行通信,我似乎需要做些什么。
java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread-4
是我得到的例外。对于我遇到的这个问题,有没有简单的解决方法?
相关代码:
//Socket Class
/**
*Initiates the socket
*/
public void run() {
this.startSocket(); *5
}
/**
* Listens for user input
*/
private void startSocket() {
String input;
while(true) {
input=scanner.next();
handle(input); *4
}
}
/**
* Handles user input
*/
private void handle(String s) {
String[] a = s.split(",");
int[] ab = {Integer.parseInt(a[0]),
Integer.parseInt(a[1]),Integer.parseInt(a[2])};
clientController.changeLightSequence(ab[0], ab[1], ab[2]); *3
}
//Controller Class
/**
*Sends a command to GUI
*/
public void changeLightSequence(int red, int yellow, int green) {
clientGUI.changeLightSequence(red, yellow, green); *2
}
//GUI class
/**
* Handles user input-command.
*/
public void changeLightSequence(int red, int yellow, int green) {
//setRunningTimeLine(red, yellow, green);
hostField.setText("Hello world!"); *1
animation();
}
使用我的相关代码堆栈跟踪:
at Client.ClientGUI.changeLightSequence(ClientGUI.java:225) *1
at Client.ClientController.changeLightSequence(ClientController.java:26) *2
at Client.ClientSocket.handle(ClientSocket.java:120) *3
at Client.ClientSocket.startSocket(ClientSocket.java:83) *4
at Client.ClientSocket.run(ClientSocket.java:88) *5