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 7964867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:00:21+00:00 2026-06-04T06:00:21+00:00

Maybe it’s a bad idea, but I wrote a subclass for JMenuItem to add

  • 0

Maybe it’s a bad idea, but I wrote a subclass for JMenuItem to add a new constructor. Let’s call it JMenuItemX:

import java.awt.event.ActionListener;
import javax.swing.JMenuItem;

@SuppressWarnings("serial")
public class JMenuItemX extends JMenuItem
{
  public JMenuItemX(final String the_text, final char the_mnemonic,
                    final ActionListener the_action)
  {
    super(the_text);
    setMnemonic(the_mnemonic);
    addActionListener(the_action);
  }
}

Now, I am trying to add a tooltip to the component by using AbstractAction:

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class AboutAction extends AbstractAction
{

  public AboutAction()
  {
    super("About");
    putValue(SHORT_DESCRIPTION, "Show the popup window");
  }

  @Override
  public void actionPerformed(final ActionEvent the_event)
  {
    JOptionPane.showMessageDialog(null, "Hello World!");
  }
}

and constructing the component as so (the JFrame, JMenuBar, and JMenu are already set up):

menu.add(new JMenuItemX("About", 'a', new AboutAction()));

(As you can see, if I am adding multiple menu items, this single-line format is easier to read.)


The menu item is visible with the correct text, and the mnemonic and action work as expected. But there is no tooltip… However, if I simply use JMenuItem, the tooltip works!

JMenuItem about = new JMenuItem(new AboutAction());
about.setMnemonic('a');
menu.add(about);

How can I get AbstractAction to work with my JMenuItem subclass?

  • 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-04T06:00:23+00:00Added an answer on June 4, 2026 at 6:00 am

    Your overridden constructor doesn’t use the JMenuItem constructor taking an Action as argument, and doesn’t call setAction() either. So your menu item is not associated to the action.

    I would not create a subclass just to add a constructor. Why not simply create a factory method?

    public JMenuItem createMenuItemWithListener(String text, 
                                                char mnemonic, 
                                                ActionListener action) {
        JMenuItem result = new JMenuItem(text);
        result.setMnemonic(mnemonic);
        result.addActionListener(action);
        return result;
    }
    
    public JMenuItem createMenuItemWithAction(char mnemonic, 
                                              Action action) {
        JMenuItem result = new JMenuItem(action);
        result.setMnemonic(mnemonic);
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

maybe it's dumb but is there a difference between new Something().method(); and Something tmpSomething
Maybe I'm having a really bad day, but could someone possibly help me to
Maybe that's only possible with SQLITE but is there also a trick to let
Maybe a silly question but here goes anyway. Example: Let's say I have one
Maybe it's my query, but I don't think so. I'm attempting to import messages
Maybe I'm forgetting an import at the head, but nothing seems to work. I'm
Maybe its a very dumb question but I hope you can give me some
Maybe I'm missing it somewhere in the PHP manual, but what exactly is the
Maybe I should first give an idea of what I want to accomplish as
Maybe there is no difference, but is either way better than the other (or

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.