title: ActionListener for Buttons
body: <p>So I have developed a program for a class of mine to use ActionListener for events when a button is clicked. I have the program working however when clicking any button you have to click the input buttons multiple times for the answer to register and move to the next level of the program.</p>
  
  <p>example1: Alert button open's 2 sometimes 3 "Hey There's a bug on you!" message dialogs. </p>
  
  <p>example2: Yes/No prompts you 3-4 times to return your yes/no answer to txt.</p>
  
  <p>example3: Color takes 3/4 clicks before returning the input to txt.</p>
  
  <p>(I think you get the picture...)</p>
  
  <p>For the life of me I cannot find out why it won't just take one input and move on...</p>
  
  <p>The code of my program for your review... Thank you in advanced.</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  
  
  public class MessageBoxes{
  
    private JButton alert = new JButton("Alert");
    private JButton yesNo = new JButton("Yes/No");
    private JButton color = new JButton("Color");
    private JButton vals = new JButton("3 Vals");
    private JButton input = new JButton("Input");
  
    private JTextField txt = new JTextField(15);
  
    private JFrame frame;
  
    private ActionListener al = new ActionListener(){
      public void actionPerformed(ActionEvent e){
  
      alert.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
          {
              //Execute when button is pressed
              JOptionPane.showMessageDialog(null,
                                    "There's a bug on you!", "Hey!",
                                    JOptionPane.ERROR_MESSAGE);
          }
      });  
  
      yesNo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
          {
              //Execute when button is pressed
              int val = JOptionPane.showConfirmDialog(null,
                                    "Choose yes or no", "Your Call...",
                                    JOptionPane.YES_NO_OPTION);
  
              if(val != JOptionPane.CLOSED_OPTION){
                if(val == 0){
                  txt.setText("Yes");
              }
              else{
                  txt.setText("No");
              }
              }
          }
      });  
  
      color.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
          {
              //Execute when button is pressed
              Object[] options = {"Red", "Green"};
  
              int sel = JOptionPane.showOptionDialog(null,
                              "Choose a Color!", "Warning",
                              JOptionPane.DEFAULT_OPTION,
                              JOptionPane.WARNING_MESSAGE, null,
                                          options, options[0]);
  
              if(sel != JOptionPane.CLOSED_OPTION){
                  txt.setText("Color Selected: " + options[sel]);
              }
          }
      });  
  
      vals.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
          {
              //Execute when button is pressed
              Object[] selections = {"First", "Second", "Third"};
  
              Object val = JOptionPane.showInputDialog(null,
                                               "Choose one", "Input",
                                               JOptionPane.INFORMATION_MESSAGE,
                                               null, selections, selections[0]);
  
              if(val != null){
                  txt.setText(val.toString());
              }
          }
      });
  
      input.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
          {
              //Execute when button is pressed
              String val = JOptionPane.showInputDialog("How mant fingers do you see?");
  
                  txt.setText(val);
          }
      });  
    }
    };
  
  
  
    public MessageBoxes(){
  
      frame = new JFrame("Title");
  
      frame.setSize(250, 250);
  
      frame.getContentPane().setLayout(new FlowLayout());
  
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      frame.getContentPane().add(alert);
      frame.getContentPane().add(yesNo);
      frame.getContentPane().add(color);
      frame.getContentPane().add(vals);
      frame.getContentPane().add(input);
      frame.getContentPane().add(txt);
  
      frame.setVisible(true);
  
    }
  
    public void launchJFrame(){
  
      alert.addActionListener(al);
      yesNo.addActionListener(al);
      color.addActionListener(al);
      vals.addActionListener(al);
      input.addActionListener(al);
  
  }
  
   public static void main(String [] args){
  
      MessageBoxes messageBoxes = new MessageBoxes();
      messageBoxes.launchJFrame();
   }
  }
  </code></pre>