title: Method for MouseListeners and KeyListeners not working with .addMouseListener for Buttons on a Jpanel, Java
body: <p>I have been having difficulty with making a method of Listeners to use repetitively in my code. Also, I am new to this, so I am very sorry for anything I leave out on this. Though, now here is my method for the Listeners:</p>
  
  <pre><code>public static void keysEvents(Optional&lt;String&gt; output)
  {
      MouseListener mouseEvents = new MouseAdapter()
      {
          public void mouseClick (MouseEvent mouseEvent)
          {
              Integer mouseModifiers = mouseEvent.getModifiers();
              if ((mouseModifiers &amp; InputEvent.BUTTON1_MASK) == 
              InputEvent.BUTTON1_MASK)
              {
                  if (output == null)
                  {
                      System.out.println("click");
                  }
                  //more options...
              }
          }
          public void mouseRelease (MouseEvent mouseEvent)
          {
  
          }
      };
      //More listeners...
  }
  </code></pre>
  
  <p>EDITED VERSION:</p>
  
  <pre><code>public static MouseListener keysEvents(Optional&lt;String&gt; output)
  {
      MouseListener mouseEvents = new MouseAdapter()
      {
          public void mouseClick (MouseEvent mouseEvent)
          {
              Integer mouseModifiers = mouseEvent.getModifiers();
              if ((mouseModifiers &amp; InputEvent.BUTTON1_MASK) == 
              InputEvent.BUTTON1_MASK)
              {
                  if (output == null)
                  {
                      System.out.println("click");
                  }
                  //more options...
              }
          }
          public void mouseRelease (MouseEvent mouseEvent)
          {
  
          }
      };
      //More listeners...
      return mouseEvents;
      //How would I have it return different listeners?
  }
  </code></pre>
  
  <p>Here is the code for the program's window:</p>
  
  <pre><code>    JPanel MainP = new JPanel();
      MainP.setLayout(new GridLayout(4, 2, 100, 30));
      frame.setLayout(new GridLayout(1, 2));
      frame.setBackground(Color.blue);
      JPanel _B1_ = new JPanel();
      JPanel _B2_ = new JPanel();
      JPanel _B3_ = new JPanel();
      JPanel _B4_ = new JPanel();
      Button _Continue_ = new Button("Continue");
      Button _Load_Game_ = new Button("Load Game");
      Button _Settings_ = new Button("Settings");
      Button _Exit_ = new Button("Exit");
      _Continue_.addMouseListener(keysEvents(Optional.of(""))); //&lt;edited here.
      _Continue_.setBackground(Color.lightGray);
      _Load_Game_.setBackground(Color.lightGray);
      _Settings_.setBackground(Color.lightGray);
      _Exit_.setBackground(Color.lightGray);
      MainP.setBackground(Color.gray);
      _B1_.setBackground(Color.gray);
      _B2_.setBackground(Color.gray);
      _B3_.setBackground(Color.gray);
      _B4_.setBackground(Color.gray);
      MainP.add(_B1_);
      MainP.add(_Continue_);
      MainP.add(_B2_);
      MainP.add(_Load_Game_);
      MainP.add(_B3_);
      MainP.add(_Settings_);
      MainP.add(_B4_);
      MainP.add(_Exit_);
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setUndecorated(!frameBorder);
      frame.pack();
      frame.setMinimumSize(new Dimension(_minX, _minY));
      frame.setSize(windowX, windowY);
      frame.add(MainP);
      frame.setVisible(true);
  </code></pre>
  
  <p>Finally, if I asked wrongly in any way, please tell me; so I can do better, later on, when asking questions. Hope it does not suck hours out of your life like it did me.</p>