Drextor Drextor - 1 month ago 10
Java Question

Java - Parsing Method Calls to other Class

I want to outsource my Menu-Creater, so i create the Class Menuhelper.

There i have a Method named "addActionListener" who adds a Action to the MenuItems

public static void addActionListener(JMenuItem menuItem ) //Need parsed Method
{

menuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//MethodeCall
}
});
}


How can i parse the Method who i need ? As String ?

Answer

You can use callback method or java 8 lambda expression.

  public static void addActionListener(JMenuItem menuItem, Callback callback ) //Need parsed Method
  {

    menuItem.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        callback.doAction();
      }
    });
  }