Abdullah Sorathia
Abdullah Sorathia

Reputation: 91

Java GUI Freezes

I am trying to create a Simple TCP Client Server Application Interface where user can start or stop the server when they pres the respective buttons I have created a StartServer Button when user presses the button it should connected to the server the problem am facing is when the user clicks the button noting happens and the interface halts

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    DefaultListModel dlm=new DefaultListModel();
    String clientSentence; 
    String capitalizedSentence;
    try {
        welcomeSocket= new ServerSocket(6789);
        dlm.addElement("server started..");            
        dlm.addElement("Server Waiting for Connections on Port 6789");
        jList1.setModel(dlm); 
        displayfull();
         while(true)
        {                
        DataOutputStream outToClient = null;
        try {


            Socket connectionSocket = welcomeSocket.accept();
            //dlm.addElement("Client Connected ");
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            //dlm.addElement("Obtained a handle on Client Input Stream");
            outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            //dlm.addElement("Obtained a handle on Client Output Stream");
            clientSentence = inFromClient.readLine();
            //dlm.addElement("Received: " + clientSentence);
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
            //dlm.addElement("Message Sent");
        } catch (Exception e) {

        } 

    }               

    } catch (Exception e) {
        dlm.addElement(e);
    }
   jList1.setModel(dlm); 
    displayfull();
}   

enter image description here

Upvotes: 0

Views: 840

Answers (1)

pimaster
pimaster

Reputation: 1967

As others have hinted at, you are spending precious time on the Event Dispatch Thread.
If you read through the tutorial on how to do Concurrency in Swing you will discover you are given tools like the Swing worker.

There are many questions like this on Stack Overflow, some more helpful than others.

Upvotes: 2

Related Questions