1

I am trying to learn network programming. I was given the task to create a single threaded client server chat application using java swing. I am having problem in updating the GUI. I have taken a textarea to display messages and another textarea to send messages. there is a button that you click to send message. I am using timer to update GUI every 5 seconds. Application runs. GUI updates initially and then screen freezes. Can anyone help me? I would like to know where I am going wrong.

package chattingapplication;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;

/**
 *
 * @author Qanita
 */
public class ServerMessenger extends javax.swing.JFrame {
    private int port;
    private ServerSocket server;
    private Socket client;
    private BufferedReader clientMessage;
    private PrintWriter serverMessage;
    private Timer timer;
    private ActionListener updateDisplay;
    private String receivingMessage;
    private String sendingMessage;

/**
 * Creates new form ServerMessenger
 */
public ServerMessenger() {
    initComponents();
    port = 13;
    try {
        server = new ServerSocket(port);
        client = null;
        client = server.accept();
        clientMessage = new BufferedReader(new InputStreamReader(client.getInputStream()));
        serverMessage = new PrintWriter(client.getOutputStream(), true);
        updateDisplay = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                receiveMessage();
                displayClientMessage();
                System.out.println("\nin timer");
            }
        };
        timer = new Timer(5000, updateDisplay);
        timer.setRepeats(true);
        timer.start();
    }
    catch (Exception e) {
        System.err.println(e);
    }
}

private void displayClientMessage()
{
    if ( receivingMessage != null )
    {
        receivingMessage = "\nClient : " + receivingMessage;
        displayArea.setText( displayArea.getText() + receivingMessage );
    }
}
private void displayServerMessage()
{
    sendingMessage = "\nServer : " + sendingMessage;
    displayArea.setText( displayArea.getText() + sendingMessage );
}
private void sendMessage()
{
    sendingMessage = writingArea.getText();
    serverMessage.println(sendingMessage);
}
private void receiveMessage()
{
    try {
        receivingMessage = clientMessage.readLine();
    } catch (IOException ex) {
        Logger.getLogger(ServerMessenger.class.getName()).log(Level.SEVERE, null, ex);
    }
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jScrollPane2 = new javax.swing.JScrollPane();
    displayArea = new javax.swing.JTextArea();
    jScrollPane1 = new javax.swing.JScrollPane();
    writingArea = new javax.swing.JTextArea();
    enter = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Server");
    setResizable(false);

    displayArea.setEditable(false);
    displayArea.setColumns(20);
    displayArea.setLineWrap(true);
    displayArea.setRows(5);
    jScrollPane2.setViewportView(displayArea);

    writingArea.setColumns(20);
    writingArea.setRows(5);
    jScrollPane1.setViewportView(writingArea);

    enter.setText("Send");
    enter.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            enterActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jScrollPane2)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(enter, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 0, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addComponent(enter, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );

    pack();
}// </editor-fold>                        

private void enterActionPerformed(java.awt.event.ActionEvent evt) {                                      
    sendMessage();
    writingArea.setText("");
    displayServerMessage();
}                                     

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(ServerMessenger.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(ServerMessenger.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(ServerMessenger.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(ServerMessenger.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new ServerMessenger().setVisible(true);
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JTextArea displayArea;
private javax.swing.JButton enter;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea writingArea;
// End of variables declaration                   

}

the client class is almost same in logic with subtle difference. But logic is almost same. I guess the issue is with timer but what exactly the issue is I am unable to figure out. Any help in this regard will highly be appreciated.

  • 2
    use SwingWorker, otherwise all output to the Swing GUI must be wrapped into invokeLater – mKorbel Mar 23 '13 at 17:09
  • i don't know if this has been posted before. I searched and I couldn't find the solution to my problem. If anybody thinks a question like this has been posted before then plzzz provide the link to it. – Qanita Zakir Mar 23 '13 at 17:11
  • 2
    if u need it single threaded, then even timer cant be used. – Ankit Mar 23 '13 at 17:11
  • 2
    This [thread](http://stackoverflow.com/q/9240308/1057230) might be of some help to you :-) Though as rightly pointed out by **mKorbel**, do give heed to EDT thingy. Just wrap everything related to the **VIEW**, in any Thread, inside the `SwingUtilities.invokeLater(new Runnable(){here goes your update code})` – nIcE cOw Mar 23 '13 at 17:11
  • @mKorbel will you please explain a little. – Qanita Zakir Mar 23 '13 at 17:13
  • then what should i use? i want the gui to be updated every few seconds – Qanita Zakir Mar 23 '13 at 17:14
  • I have done this in console. I am having problems in gui :( – Qanita Zakir Mar 23 '13 at 17:15
  • I guess you should keep reading from the socket in one infinite loop thread.And In other thread after each update in GUI with the data received from socket in EDT , put it on `wait` until it is notified by the thread reading from socket using `notify()`after it reads the data. – Vishal K Mar 23 '13 at 17:42
  • 2
    For [example](http://stackoverflow.com/a/3245805/230513). – trashgod Mar 23 '13 at 18:50

1 Answers1

1

BufferedReader.readLine() internally calls java.io.Reader.read(char[], int, int). Accordingly with Reader.read javadoc: "This method will block until some input is available".

You should be freezing because updateDisplay() calls receiveMessage() which calls readLine() and no data should be available.

DiogoSantana
  • 2,404
  • 2
  • 19
  • 24