[英]Java - How can I append to a jTextArea from a socket?
So, I decided to try to work on a ServerSocket/Socket instant messenger to get familiarized with how to use ServerSockets and Sockets. 因此,我决定尝试在ServerSocket / Socket即时通讯程序上工作,以熟悉如何使用ServerSockets和Sockets。 I am currently still working on the server side of the application, and am having trouble getting a jTextArea on the GUI to update with information sent from a client program. 我目前仍在应用程序的服务器端工作,并且无法在GUI上获取jTextArea来更新从客户端程序发送的信息。
Here is my code: 这是我的代码:
For the Socket side of things (ServerSocket class): 对于事物的Socket方面(ServerSocket类):
while (shouldRun) {
inputMessage = (String)input.readObject();
new jMessengerServerGUI().updateFeed(inputMessage);
System.out.println(inputMessage);
}
And for the GUI (jMessengerServerGUI class): 对于GUI(jMessengerServerGUI类):
public void updateFeed(String updateMessage) {
txtServerFeed.append(updateMessage);
System.out.println(updateMessage);
}
The methods are in separate classes, where the string inputMessage is received and sent flawlessly and is printed out in the console. 这些方法位于单独的类中,在其中可以完美地接收和发送字符串inputMessage,并在控制台中将其打印出来。 The issue comes with updating the jTextArea txtServerFeed. 问题在于更新jTextArea txtServerFeed。
How can I best achieve writing to the TextArea when called from an outside class? 从外部类调用时,如何最好地实现对TextArea的写入?
You Need to make your Application Multi-threaded. 您需要使您的应用程序成为多线程。 Where Server & Socket works separate thread rather than UI-Thread; Server&Socket工作在单独的线程而不是UI-Thread的地方;
You can See that example: https://github.com/khirulbasher/Multithreaded-Server-Client 您可以看到该示例: https : //github.com/khirulbasher/Multithreaded-Server-Client
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.