简体   繁体   中英

How to display data from one object in JTextArea of another object?

I have one class named handler and this class process the http request comming from the browser and i want to display the http headers of the request in JTextArea of another class named HttpHeadersFrame! This is what i have tried

 public class Handler
 {
     HttpHeadersFrame headersFrame; //This frame contains JTextArea component
     private Request request = null;
     public String requestMessage;
     private Socket socket = null;

     public Handler(Socket socket)
     {
         this.socket = socket;
         this.headersFrame = new HttpHeadersFrame();
         headersFrame.setVisible(true);
     }

     public void processRequest()
     {
        requestMessage = request.toString(System.getProperty("line.separator"));
        headersFrame.getRequestTextArea().append(requestMessage);
     }
 }

When i run the proxy i don't get any message in JTextArea! Any help would be appreciated

In your processRequest method you create a new HttpHeadersFrame on each incoming request. Unless you display all those frames, you will constantly updating a non-visible frame instead of the one-and-only visible one. So pass a visible HttpHeadersFrame instance to your Handler class iso creating new instances and your problem will be solved.

Oh, and please make sure you update the Swing components on the Event Dispatch Thread.

Firstly, there is lack of proper usage of Swing components in your snippet. You should initialize the requestTextArea field in constructor before using it. And also you have to add it to frame with a statement

this.getContentPane().add(requestTextArea);

You should pass HttpHeadersFrame instance to your Handler class, make this instance visible, ie

headersFrame = new HttpHeadersFrame();
headersFrame.setVisible(); 

And appending requestMessage to this instance's textArea field will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM