title: Java swing GUI freezes
body: <p>I am writing a Java client/server GUI application using sockets and here is the problem:  </p>
  
  <p>I have a button to start listening for a specified port:</p>
  
  <p>button actionPerformed method</p>
  
  <pre><code>private void listenButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
      int port = Integer.parseInt(portTextfield.getText(), 10);
  
      try {
          socket.listen(port);
      } catch (IOException ex) {
      }
  }
  </code></pre>
  
  <p>Here is the socket.listen method</p>
  
  <pre><code>public static void listen() throws IOException {
      ServerSocket ss = new ServerSocket(port);
  
      while (true)
          new socket(ss.accept());
  }
  </code></pre>
  
  <p>"socket" class extends "Thread"<br>
  So after ss.accept() returns a value it creates new socket instance in separate thread.</p>
  
  <p>After clicking the button the GUI freezes because inside the socket.listen method there is an infinite loop. How can I avoid that?</p>