Search code examples
javaswingjframejtextarea

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


Solution

  • 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, i.e.

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

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