Java – how to send a string from a worker thread to a text area?
•
Java
public class Client1 implements Runnable{
public class Client1 implements Runnable{ ServerSocket serverSocket = null; Socket socket = null; DataInputStream dataInputStream = null; DataOutputStream dataOutputStream = null; String Buffer; TextArea ta; Handler mhandler; public Client1() { System.out.println("in constructor"); EstablishConnection(); Buffer = new String(); // mhandler = handler; // ta = t; } private boolean EstablishConnection() { try { System.out.println("calling socket"); socket = new Socket("192.168.1.145",8080); if(socket != null) { System.out.println("ContentApp"+ "Socket Successfully created"); } } catch (IOException e) { System.out.println("ContentApp"+ "Socket IOException"); e.printStackTrace(); } try { dataInputStream = new DataInputStream(socket.getInputStream()); System.out.println("ContentApp"+ "DataInputstream Successfully created"); } catch (IOException e) { System.out.println("ContentApp"+ "Datainputstream Failed"); e.printStackTrace(); return false; } try { dataOutputStream = new DataOutputStream(socket.getOutputStream()); dataOutputStream.writeUTF("Hi This is Hinar!"); System.out.println("ContentApp"+ "Dataoutputstream Successfully created"); } catch (IOException e) { System.out.println("ContentApp"+ "Dataoutputstream Failed"); e.printStackTrace(); return false; } if(socket != null) { run(); } return true; } public void run() { while(true) { System.out.println("ContentApp"+ "Thread is running Succesfully in loop"); try { System.out.println("reading from socket"); Buffer = dataInputStream.readUTF(); System.out.println(Buffer+"this is the data"); Client.tarea.append(Buffer);// Text area of Frames /*ta.setVisible(true); ta.setText(Buffer+"this is the Buffer");*/ } catch (IOException e) { System.out.println("ContentApp"+ "Read IO Exception"); e.printStackTrace(); } } } }
Solution
Since you didn't post any exceptions or tell us what actually went wrong, I can only guess
Look at the invokeandwait or invokelater methods in the swingutilities class:
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码