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.