title: How can jButton1ActionPerformed and jButton2ActionPerformed share a BufferedReader/BufferedWriter?
body: <p>Well I created a program using sockets with the intent for an IRC Client. jButton2 is supposed to submit messages. jButton1 is to connect to the IRC Server.  <a href="http://i.stack.imgur.com/SiUO7.png" rel="nofollow">http://i.stack.imgur.com/SiUO7.png</a>
  Is there any way for me to make the variable public? Any time I try to make it public or private or final or static or move it, there is always some very strange issues. </p>
  
  <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      try {
          String server = jTextField2.getText();
          String nick = jTextField4.getText();
          String login = jTextField5.getText();
          String pass = jTextField6.getText();
          String channel = jTextField3.getText();
          Socket socket = new Socket(server, 6667);
          BufferedWriter writer = new BufferedWriter(
                  new OutputStreamWriter(socket.getOutputStream()));
          BufferedReader reader = new BufferedReader(
                  new InputStreamReader(socket.getInputStream()));
          writer.write("NICK " + nick + "\r\n");
          writer.write("USER " + login + " 0 * Programming Bot" + "\r\n");
          if (pass.equals("")) {
          } else {
              writer.write("nickserv IDENTIFY " + pass + "\r\n");
              writer.write("ns set autoop on" + "\r\n");
          }
          writer.flush();
          String line;
          while ((line = reader.readLine()) != null) {
              jTextArea1.append(line + "\r\n");
              // 004 indicates we are logged in
              if (line.indexOf("004") &gt;= 0) {
                  // We are now logged in so we can leave the loop
                  break;
              } else if (line.indexOf("433") &gt;= 0) {
                  jTextArea1.append("Nickname is already in use.\r\n");
                  return;
              }
          }
  
          // Join the channel.
          writer.write("JOIN " + channel + "\r\n");
          writer.flush();
  
          Thread bl = new Thread(new BotListenerThread(reader, writer, channel));
          bl.start();
          writer.write("PRIVMSG dema Hello\u00A0There." + "\r\n");
  
      } catch (IOException ex) {
          Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
      }
  
  }                                        
  
  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  // Cannot access reader here
  //writer.write("PRIVMSG dema "+jTextField1.getText()+"\r\n");
  //writer.flush();
  }          
  </code></pre>
  
  <p>Then the BotListenerThread is to receive and send back ping/pong as well as to put the output text into my textArea but due to it being in a different file I can not seem to find a way to modify the jTextArea from this... Any tips for  this??</p>
  
  <pre><code>package ambassadorirc;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.IOException;
  
  public class BotListenerThread implements Runnable // threads implement the runnable interface
  {
  BufferedReader reader;
  BufferedWriter writer;
  String channel;
  
  // our constructor requires the reader, writer and channel from the main program
  public BotListenerThread(BufferedReader reader, BufferedWriter writer, String channel) {
      this.reader = reader;
      this.writer = writer;
      this.channel = channel;
  }
  
  public void run() {
      System.out.println("Listener ready...");
      String line = "";
      // Keep reading lines from the server.
      try {
          // again we use the infinite loop
          while ((line = reader.readLine()) != null) 
          {
              if (line.toLowerCase().startsWith("ping ")) {
                  // We must respond to PINGs to avoid being disconnected.
                  writer.write("PONG " + line.substring(5, line.length()) + "\r\n");                              
                                      // every time you issue a command you MUST flush to send it
                  writer.flush();
              }
              // the next lines are a VERY unsafe implementation of an auto-op script ;)
              else if (line.contains("myNich") &amp;&amp; line.contains("JOIN :"+channel))
              {
                  System.out.println("Opping myNick");
                  writer.write("MODE "+channel+" +o myNick\r\n");
                  writer.flush();
              }
              else {
                  // print the line received from the server
                  System.out.println(line);
              }
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>