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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:12:18+00:00 2026-05-11T02:12:18+00:00

In the past, when one made a JPopupMenu visible it’s first item would get

  • 0

In the past, when one made a JPopupMenu visible it’s first item would get selected by default: http://weblogs.java.net/blog/alexfromsun/archive/2008/02/jtrayicon_updat.html

Nowadays the default behavior is to pop up the menu without any item selected. I would like create a JPopupMenu with a single item that will pop up selected and centered under the mouse pointer. I have managed to get the item to pop up centered under the mouse but I the JMenuItem refuses to render as if it is selected. If I move the mouse out of the item and back in it selects properly.

Any ideas?

Here is my testcase:

import java.awt.Component; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JPopupMenu;  public class Test extends JFrame {     public static void main(String[] args)     {         JFrame frame = new JFrame();         frame.setSize(800, 600);         frame.getContentPane().addMouseListener(new MouseAdapter()         {             @Override             public void mousePressed(MouseEvent e)             {                 if (e.isPopupTrigger())                     popupTriggered(e);             }              @Override             public void mouseReleased(MouseEvent e)             {                 if (e.isPopupTrigger())                     popupTriggered(e);             }              private void popupTriggered(MouseEvent e)             {                 JPopupMenu menu = new JPopupMenu();                 final JMenuItem item = new JMenuItem('This is a JMenuItem');                 menu.add(item);                 Point point = e.getPoint();                 int x = point.x - (item.getPreferredSize().width / 2);                 int y = point.y - (item.getPreferredSize().height / 2);                 menu.show((Component) e.getSource(), x, y);             }         });         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);         frame.setVisible(true);     } } 
  • 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-11T02:12:19+00:00Added an answer on May 11, 2026 at 2:12 am

    Nowadays the default behavior is to pop up the menu without any item selected.

    Actually, I would argue that this is the correct behavior, at least in Windows. Other non-Java applications do this too. I don’t think it’s worth breaking this convention even if there is only one item in the menu. If you feel otherwise, you can set the selection index as in sean.bright’s answer.


    So, I finally got the chance to try it out on Java 1.6.0_11, and found some inconsistent behavior: If the popup menu juts out of the parent frame, the item is automatically selected; if the popup menu appears entirely within the parent frame, nothing is selected. Sounds like a Swing bug, which at least warrants an RFE for consistent behavior.

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

Sidebar

Related Questions

No related questions found

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.