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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:42:44+00:00 2026-05-22T11:42:44+00:00

here is my problem: I have a jList and a popup menu. When I

  • 0

here is my problem:
I have a jList and a popup menu. When I right click the jList, the popup menu shows. The problem is that the jList item which the mouse is pointing at won’t select.
And I want it to do that. When I point my cursor at an item in the list and press the right button, I want two things to happen. Select the item on which I clicked and show the popup menu.

I tried this:

jLists.addMouseListener(new MouseAdapter() {

     @Override
     public void mousePressed(MouseEvent e) {
            jList.setSelectedIndex(jList.locationToIndex(e.getPoint()));
     }
});

jList.setComponentPopupMenu(jPopupMenu);

But it only shows the popup menu.
If I delete this line:

jList.setComponentPopupMenu(jPopupMenu);

then the right-click select works (but the popup menu doesn’t show).

So, what do you think is the best way to make these two functions (both) work ?

Thanks and sorry for my english.

  • 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-05-22T11:42:45+00:00Added an answer on May 22, 2026 at 11:42 am

    Don’t do setComponentPopupMenu. In the MouseAdapter do the following:

    public void mousePressed(MouseEvent e)  {check(e);}
    public void mouseReleased(MouseEvent e) {check(e);}
    
    public void check(MouseEvent e) {
        if (e.isPopupTrigger()) { //if the event shows the menu
            jList.setSelectedIndex(jList.locationToIndex(e.getPoint())); //select the item
            jPopupMenu.show(jList, e.getX(), e.getY()); //and show the menu
        }
    }
    

    This should work.

    EDIT: The code now checks both press and release events, because some platforms show popups when mouse presses and some other on release. See the Swing tutorial for more info.

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

Sidebar

Related Questions

Here's my problem: I have a virtual method defined in a .h file that
I'm struggling here with a problem: I have a controller questions which has action
Here's the problem: 1.) We have page here... www.blah.com/mypage.html 2.) That page requests a
Here is the problem I have: I have a lot (tens of thousands) of
Here's my problem - I have some code like this: <mx:Canvas width=300 height=300> <mx:Button
Here is the problem: we have lots of Javascripts and lots of CSS files,
Here's my problem: I have to call a web service with a secure header
Here's my problem.I have 2 xmlfiles with identical structure, with the second xml containing
Here is the problem: We have all of our development under subversion, but our
Well here's my problem I have three tables; regions, countries, states. Countries can be

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.