title: Sending message to a Client with JButton in java
body: <p>I have a Java program where run-time JButtons are generated whenever a new client connects to my server program. I need to send a message to a particular client only when a JButton is clicked which is assigned for that particular Client.</p>
  
  <pre><code>void connect_clients()
      {
          try {
              ServerSocket listener = new ServerSocket(7700);
              jButton2.setText("Server Running!");
              jButton2.setEnabled(false);
              try {
                  while (true) {
  
                      socket = listener.accept();
                      try {
  
                             //  System.out.println("Client connected from " + socket.getLocalAddress().getHostName());
                              clientIP= socket.getLocalAddress().getHostName();
                              buttons.add(new JButton(clientIP));
                             displayButton(buttons.get(clientNumber),clientNumber);
                              clientNumber++;
  
                   } 
                      finally {
                          socket.close();
                      }
                  }
              }
              finally {
                  listener.close();
              }
          }
          catch (IOException ex) {
              Logger.getLogger(Test_Frame.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  </code></pre>
  
  <p>I tried something like this.</p>
  
  <pre><code>public void actionPerformed( ActionEvent e ) throws IOException{
      if ( e.getSource() == buttons.get(1) )
      {
          String thisIP = buttons.get(1).getText();
          PrintWriter out =
                          new PrintWriter(socket.getOutputStream(), true);
  
                           out.println("hey");
                           JOptionPane.showMessageDialog(null,"gfb");
      }
    }
  </code></pre>
  
  <p>These are declared gobally:</p>
  
  <pre><code>String clientIP = "";
      int clientNumber = 0;
      ArrayList&lt;JButton&gt; buttons = new ArrayList&lt;JButton&gt;();
      Socket socket;
  </code></pre>