title: Java Best way to invoke methods with a given string
body: <p>So I'm making a custom menu and this is the sub-menu item class which handles invoking the correct method. Currently I am using a switch statement for the method name, but is it the best way to do it? </p>
  
  <p>I would love a new set of eyes, I have been looking at this way to long. Thanks!</p>
  
  <pre><code>import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import javax.swing.JLabel;
  
  import com.as.devcenter.Util;
  
  public class MenuSubItem extends JLabel implements MouseListener {
  
      private static final long serialVersionUID = 1L;
  
      private String action = "";
  
      public MenuSubItem(String label, String action, Class&lt;?&gt; className) {
      addMouseListener(this);
  
      this.action = action;
  
      setText(label);
      setName(action);
      setSize(Util.widthOfText(getText(), Util.getDefaultFont()) + 4, 22);
      }
  
      /*
       * Start Mouse Events
       */
  
      @Override
      public void mouseClicked(MouseEvent e) {
      Util.log("MenuSubItem clicked");
      if (action.equalsIgnoreCase("")) {
          Util.log("No action was provided for MenuSubItem: " + getText());
      } else {
          try {
          Method method = Actions.class.getDeclaredMethod(action, className);
  
          switch (method.getName()) {
          case "newProject":
              NewProjectFrame npf = new NewProjectFrame();
              method.invoke(npf, npf);
              break;
          case "openConsole":
              Console console = new Console();
              method.invoke(console, console);
              break;
          }
          } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
          if (e1 instanceof NoSuchMethodException) {
              Util.log("Could not identify action: " + action);
          } else {
              e1.printStackTrace();
          }
          }
  
      }
      }
  
      public void mousePressed(MouseEvent e) {
      }
  
      public void mouseReleased(MouseEvent e) {
      }
  
      public void mouseEntered(MouseEvent e) {
      }
  
      public void mouseExited(MouseEvent e) {
      }
  }
  </code></pre>
  
  <p>Actions class (just have 2 methods atm but will have more)</p>
  
  <pre><code>public class Actions {
  
      public static void newProject(NewProjectFrame npf){
          npf.setup();
          Util.getProgram().items[0].clear();
      }
  
      public static void openConsole(Console console){
      console.setup();
      Util.getProgram().items[0].clear();
      }
  }
  </code></pre>