title: Same actionhandler, multiple buttons
body: <p>I'm using JFrame to create an application and I want to know how it's possible to set the same actionhandler to different buttons. In my application I got 8 buttons and once you click on them, they should all disappear. This ''disappearing thing'' happens for each button, but after that each button does something different. So I want to save some lines by doing this, but I don't know how.</p>
  
  <p>This is the code to explain what I mean:</p>
  
  <pre><code>import java.awt.EventQueue;
  
  import javax.swing.JFrame;
  import javax.swing.JButton;
  import java.awt.event.ActionListener;
  import java.awt.event.ActionEvent;
  
  
  public class StackOverflowExample {
  
      private JFrame frame;
  
      /**
       * Launch the application.
       */
      public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  try {
                      StackOverflowExample window = new StackOverflowExample();
                      window.frame.setVisible(true);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          });
      }
  
      /**
       * Create the application.
       */
      public StackOverflowExample() {
          initialize();
      }
  
      /**
       * Initialize the contents of the frame.
       */
      private void initialize() {
          frame = new JFrame();
          frame.setBounds(100, 100, 450, 300);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().setLayout(null);
  
          JButton btnNewButton = new JButton("New button");
          JButton btnNewButton_1 = new JButton("New button");
          JButton btnNewButton_2 = new JButton("New button");
          JButton btnNewButton_3 = new JButton("New button");
          btnNewButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
  
  
                  initialize2();
              }
          });
          btnNewButton.setBounds(75, 51, 89, 23);
          frame.getContentPane().add(btnNewButton);
  
          btnNewButton_1.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
  
                  btnNewButton.setVisible(false);
                  btnNewButton_1.setVisible(false);
                  btnNewButton_2.setVisible(false);
                  btnNewButton_3.setVisible(false);
                  initialize2();
              }
          });
          btnNewButton_1.setBounds(75, 85, 89, 23);
          frame.getContentPane().add(btnNewButton_1);
  
  
          btnNewButton_2.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
  
                  btnNewButton.setVisible(false);
                  btnNewButton_1.setVisible(false);
                  btnNewButton_2.setVisible(false);
                  btnNewButton_3.setVisible(false);
                  initialize2();
              }
          });
          btnNewButton_2.setBounds(75, 119, 89, 23);
          frame.getContentPane().add(btnNewButton_2);
  
  
          btnNewButton_3.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
  
                  btnNewButton.setVisible(false);
                  btnNewButton_1.setVisible(false);
                  btnNewButton_2.setVisible(false);
                  btnNewButton_3.setVisible(false);
                  initialize2();
              }
          });
          btnNewButton_3.setBounds(75, 153, 89, 23);
          frame.getContentPane().add(btnNewButton_3);
      }
      private void initialize2(){
  
          /*here I'll put the code for my new frame.
            I'll make like some more buttons and a textfield. 
          */
      }
  }
  </code></pre>