user3900751
user3900751

Reputation: 115

Java - JTextArea Having issues[SOVLED]

In the code below I receive a packet, take the data and then output it to the Gui. The Problem is with "rmsg". I can System.out.println it perfectly fine but when I feed it to the "PacketRecieved" method the JTextArea disappears/looses its text. Also When I remove rmsg from the parameters it works as it is supposed to.

DatagramPacket receive = new DatagramPacket(buf, buf.length);

 socket.Receive(receive);

 String rmsg = new String(receive.getData());

 System.out.println(rmsg); //actually works and outputs the Message to the console

 gui.PacketRecieved("Received Packet From: " + recieve.getAddress() + "\n Message: " + rmsg); //causes the JTextField to mess up.

Here is the PacketRecieved Method.

public void PacketRecieved(String s){
    textArea.append(s + "\n");
}

Complete server:

public class Server {   //imports not included

private static ServerGui gui;

public static void main(String args[]){

    System.out.println("Server: Server started");

    gui = new ServerGui();

    getPackets();

}

public static void getPackets(){



    DatagramSocket socket;

    try {
        Thread.sleep(100);

         socket = new DatagramSocket(5000);

         byte buf[] = new byte[1000];

     while(true){

         DatagramPacket Recieve = new DatagramPacket(buf, buf.length);

         socket.receive(Recieve);

         String rmsg = new String(Recieve.getData());

         System.out.println("test: " + rmsg); // actually works and outputs the Message to the console

         gui.PacketRecieved("Recieved Packet From: " + Recieve.getAddress() + "\n Message: " + rmsg);  //causes the JTextField to mess up.


     }

         }catch(Exception e){
             getPackets();
         }
}   

}

complete Server GUI:

public class ServerGui extends JFrame{   //imports not included
private static final long serialVersionUID = 1L;

@SuppressWarnings("unused")

private JTextArea textArea;

public ServerGui(){
    super("Server");
    setVisible(true);
    setSize(600, 600);
    setLayout(new FlowLayout());

    textArea = new JTextArea("Recieved Packets\n", 50, 50);
    textArea.setVisible(true);
    add(textArea);
    textArea.append(" ");

}

public void PacketRecieved(String s){
    System.out.println(textArea.getText());
    textArea.append(s + "\n");
}


@Override
public void processWindowEvent(WindowEvent e){
    if(e.getID() == WindowEvent.WINDOW_CLOSING){
        System.exit(0);
    }
}

the client just sends packets with a string in it. if you really need it ill send it too.

Upvotes: 0

Views: 81

Answers (2)

user3530525
user3530525

Reputation: 691

Try putting your jtextarea in a JScrollPane. Then doing:

public void PacketRecieved(String s){
    System.out.println(textArea.getText());
    textArea.append(s.toString() + System.getProperty("line.separator"));
}

Upvotes: 1

kjohnson
kjohnson

Reputation: 110

Is the newline making the text scroll up, so it looks like it disappears? Try sending textArea.getText() to the console to verify the contents of the text area.

Upvotes: 0

Related Questions