title: Client-Server - freezing button
body: <p>I'm writing a simple client-server appa. It shoud work like that. I run the server app than i click button to create server. When server turn on i can the client app and connect to the server. Server also have JTextFild tfLog where message log pops out.
  My problem is that when i click button to create server if freez.
  <a href="http://i.stack.imgur.com/XMVaT.png" rel="nofollow">Image</a></p>
  
  <p>All looks like this:</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.WindowAdapter;
  import java.awt.event.WindowEvent;
  
  public class ServerGui extends JFrame implements ActionListener{
  
  protected JButton bCreateServer;
  public static JTextArea taLOg;
  
  public ServerGui(){
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      addWindowListener(new WindowAdapter() {
          @Override
          public void windowClosing(WindowEvent e) {
              dispose();
          }
      });
      initLayout();
      setVisible(true);
  
  
  }
  
  private void initLayout() {
      setSize(600, 400);
      setResizable(false);
      setLayout(null);
      Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
      this.setLocation(dimension.width/2-this.getSize().width/2,
              dimension.height/2 - this.getSize().height/2);
  
      bCreateServer = new JButton("Utwórz serwer");
      bCreateServer.setBounds(20,20,150,25);
      bCreateServer.addActionListener(this);
      bCreateServer.setActionCommand("Create");
      add(bCreateServer);
  
      taLOg = new JTextArea();
      taLOg.setEditable(false);
      JScrollPane pane = new JScrollPane(taLOg);
      pane.setBounds(20, 50, 400,300);
      add(pane);
  }
  
  public static void main(String args[]){
      SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
              new ServerGui();
          }
      });
  }
  
  public void insertText(final String line){
      if(SwingUtilities.isEventDispatchThread()){
          taLOg.insert(line, 0);
      }
      else{
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run(){
                  taLOg.insert(line, 0);
              }
          });
      }
  }
  
  @Override
  public void actionPerformed(ActionEvent e) {
  
      String tmp = e.getActionCommand();
      if(tmp.equals("Create")){
  
          Server server = new Server(8080);
          if(server.serverCreated){
              insertText("Kliknięto Utwórz serwer\n");
              server.clientsListener();
          }
          else{
              insertText("Nie udało się utworzyć serwera\n");
          }
  
      }
  }
  }
  
  import java.io.IOException;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class Server{
  private ServerSocket serverSocket;
  private Socket socket;
  public Boolean serverCreated = false;
  public Server(int port){
      try{
          serverSocket = new ServerSocket(port);
      }
      catch(IOException e){
          System.out.println("Błąd przy tworzeniu gniazda serwerowego.");
          System.exit(-1);
      }
      serverCreated = true;
  }
  public void clientsListener(){
      try{
          socket = serverSocket.accept();
      }
      catch(IOException e){
          System.out.println(e);
      }
  
      System.out.println(socket);
      try{
          serverSocket.close();
      }
      catch(IOException e){
          System.out.println("Błąd przy zamykaniu gniazda serwerowego.");
          System.exit(-1);
      }
  }
  }
  
  
  import java.io.IOException;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  public class Client{
  
  Socket clientSocket;
  
  public Client(String hostname, int port){
      try{
          clientSocket = new Socket(hostname, port);
      }
      catch(UnknownHostException e){
          System.out.println("Nieznany host.");
      }
      catch(IOException e){
          System.out.println(e);
          System.exit(-1);
      }
      System.out.println(clientSocket);
  }
  
  public static void main(String args[]){
      Client client = new Client("localhost", 8080);
  }
  }
  </code></pre>