Search code examples
javaswingjtextarea

JTextArea setText is not working


I have a problem with setting text in JTextArea, I tried setText(which I'd prefer) and append also. I do not know where the problem is, I got client-server app. I want to put message that server sends in JTextField but I am unable to here is my code:

client side code which is reciving the message properly:

try
    {


        Socket socket = new Socket("localhost", PORT);
        BufferedReader input = new BufferedReader( new InputStreamReader(System.in));
        DataOutputStream output = new DataOutputStream(socket.getOutputStream());
        BufferedReader serverInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        output.writeBytes(outputString + "\n");
        inputString = serverInput.readLine(); // private String inputString
        mymodel.setTextArea(inputString); // this is not working
        System.out.println(inputString); // this is working
        socket.close();

    }
    catch...

setTextArea method:

public void setTextArea(String string)
{
    MyPanel mypanel = new MyPanel();
    mypanel.textArea.setText(string); // debugger shows that the string contains message from server
}

I have set textarea to public since setter method weren't working, actually this one isn't working also. I do not know where the problem is, and debugger isn't helping me either.

Looking for your answers

EDIT:

JTextTable code:

textArea = new JTextArea(1, 30);
textArea.setEditable(false);
panel.add(textArea, c);

Solution

  • Try to get access through getter. Like

    public JTextArea getTextArea()
    {
         return jTextAreaField;
    }
    

    and then

    getTextArea().append("ur text");