Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7826079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:15:38+00:00 2026-06-02T09:15:38+00:00

Ok, I have one class (let’s call it: MenuBarClass) that contain multiple Menu and

  • 0

Ok, I have one class (let’s call it: MenuBarClass) that contain multiple Menu and MenuItem.
I whant assign to every MenuItem an actionlistener, but.. instead of doing something like:

menuitem_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {} });
menuitem_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {} });
menuitem_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {} });
// ...
menuitem_N.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {} });

I whant my code more mantainable and.. more important.. I don’t whant a lots of “if” in one huge ActionListener class like:

public void actionPerformed(ActionEvent e) {
  if (e.getSource().equals(menuitem_1)) {
    //do stuff..
  } else if (e.getSource().equals(menuitem_2)) {
    //do stuff..
  } else ...
}

how can i do this, if it is possible?
Can anyone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:15:39+00:00Added an answer on June 2, 2026 at 9:15 am

    You can reduce verbosity using the reflection API to create a utility method:

    package demo;    
    import java.awt.event.*;
    import java.lang.reflect.*;
    
    public class ListenerProxies {    
      private static final Class<?>[] INTERFACES = { ActionListener.class };
    
      public static ActionListener actionListener(final Object target,
                                                        String method) {
        final Method proxied = method(target, method);
        InvocationHandler handler = new InvocationHandler() {
          @Override
          public Object invoke(Object proxy, Method method, Object[] args)
              throws Throwable {
            ActionEvent event = (ActionEvent) args[0];
            return proxied.invoke(target, event);
          }
        };
        return (ActionListener) Proxy.newProxyInstance(target.getClass()
            .getClassLoader(), INTERFACES, handler);
      }
    
      private static Method method(Object target, String method) {
        try {
          return target.getClass().getMethod(method, ActionEvent.class);
        } catch (NoSuchMethodException e) {
          throw new IllegalStateException(e);
        } catch (SecurityException e) {
          throw new IllegalStateException(e);
        }
      }
    }
    

    This can be utilized like this:

    package demo;
    import static demo.ListenerProxies.actionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    
    public class Demo {
    
      public static void main(String[] args) {
        Demo test = new Demo();
        JButton hello = new JButton("Say Hello");
        hello.addActionListener(actionListener(test, "sayHello"));
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(hello);
        frame.pack();
        frame.setVisible(true);
      }
    
      public void sayHello(ActionEvent event) {
        System.out.println("Hello");
      }
    }
    

    The downside of this is the lack of compile-time checking that the sayHello(ActionEvent) method exists.

    The performance costs will be negligible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have one TagLib that use FormatTagLib : class MyTagLib {
In one class, I have to call a constructor of another class that needs
I have a program that will store many instances of one class, let's say
I have a class (let's call it MyService ) that accepts two dependencies in
Let's say I have one class Foo that has a bunch of logic in
I have one abstract class and two implementations. Let's call them ParentAbstract , ParentA
I have one abstract class -let's say myBase. And I want all the classes
Let we have one abstract class: classdef ACalculation < handle methods (Abstract) [result] =
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
I have only one class with many instances. Every instance is observer of couple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.