javaswinginvokelater

DatagramSocket UDP Server Swing InvokeLater not working


Trying to make a DatagramSocket UDP server/client chat app but having some problems getting the GUI to update after receiving a packet with a DatagramPacket. Here's my code; the only thing I left out, which I thought was irrelevant, was my grouplayout code for my JFrame window.

public class Server extends JFrame {

    JButton startServerButton;
    JTextField textField;
    JTextArea textArea;

    public Server() {

        super("Server");
        initComponents();
        setResizable(false);
        setVisible(true);

    }

    private void startServer() throws IOException {

        DatagramSocket serverSocket;
        String sentence;

        startServerButton.setText("Server started");
        startServerButton.setEnabled(false);

        while (true) {

            serverSocket = new DatagramSocket(9876);

            // receive
            byte[] receiveData = new byte[1024];
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            sentence = new String(receivePacket.getData());
            int port = receivePacket.getPort();
            System.out.println("RECEIVED (port " + port + "): " + sentence);
            showMessage("RECEIVED (port " + port + "): " + sentence);
            InetAddress IPAddress = receivePacket.getAddress();

            // send
            byte[] sendData = sentence.getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);
            serverSocket.close();

        }

    }

    private void showMessage(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                textArea.append("\n" + text);
            }
        });
    }

    private void initComponents() {

        startServerButton = new JButton();
        textField = new JTextField();
        textArea = new JTextArea();
        textArea.setEditable(false);



        startServerButton.setText("Start server");
        startServerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                try {
                    startServer();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


        ...grouplayout stuff here...


    }

    public static void main(String args[]) {
        Server server = new Server();
    }

}

So the problem is, the textArea on my JFrame never gets updated, but my System.out.println() always prints correctly the message received from my Client class. I can show the code for my Client class but I felt it was irrelevant. I would appreciate any help, thanks.


Solution

  • You must start your socket listener in another Thread. Otherwise you block the repainting.

    Something like this:

    startServerButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Thread t = new Thread(new Runnable() {
                public void run() {
                    try {
                        startServer();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    });