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

  • SEARCH
  • Home
  • 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 141679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:48:55+00:00 2026-05-11T07:48:55+00:00

I have a Java program where I have a JMenu with an arbitrary number

  • 0

I have a Java program where I have a JMenu with an arbitrary number of items (in this case, there is one menu item for each dynamic library currently loaded in a different program). I’m running a loop to add JCheckBoxMenuItem s to the menu, since I don’t know how many there will be.

How can I set up an action listener for these menu items that is aware of which option called it? Specifically, I want to run the same function but with a different set or parameters for each of the menu items (and a different function again depending on whether the check is being toggled or detoggled).

Could someone point me in the right direction?

  • 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. 2026-05-11T07:48:56+00:00Added an answer on May 11, 2026 at 7:48 am

    While event.getSource() will definitely let you know which particular button the event came from it has the side effect of needing to track the generated buttons or snooping into the button. Also you may want to present a different name of the library to the user (possibly including the version information) than is used to identify the library. Using the ‘ActionCommand’ property of the button may provide a way to separate those issues. So you will need to alter code in the generation of the checkbox menu items and in the listener.

    ActionListener actionListener = ... // whatever object holds the method, possibly this String[] libraries = ... // however you get your library names JMenu parentMenu = ... // the menu you are adding them to  for (String s : libraries) {   // prettyName is a method to make a pretty name, perhaps trimming off   // the leading path   JCheckBoxMenuItem child = new JCheckBoxMenuItem(prettyName(s), true);   child.setActionCommand(s);   parentMenu.acc(child); } 

    The action handler code would be…

    public void actionPerformed(ActionEvent evt) {   // the 'current' selection state, i.e. what it is going to be after the event   boolean selected = ((JCheckBoxMenuItem)evt.getSource()).isSelected();   String library = evt.getActionCommand();   ... process based on library and state here... } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java program that processes one file at a time. This Java
I have a Java program with Maven managing its dependencies. One of those dependency
I have 2 java program located seperately One in c:\test and the other in
I have java program which receives encrypted message through TCP/IP from one component of
I have a Java program which has a number options it allows the user
I Have a java program that does sort of this: It starts off with
I have a Java program that I start like this on Windows Server 2008.
I have a Java program for the graphical user interface. This Java program runs
I have a java program that has some number of classes. Three methods taken
I have this java program that I want to package within a jar. It

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.