The Artima Developer Community
Sponsored Link

Java Answers Forum
How Do I Do Right-Click Selection?

4 replies on 1 page. Most recent reply: Jun 1, 2005 4:01 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

How Do I Do Right-Click Selection? Posted: Mar 9, 2005 3:51 PM
Reply to this message Reply
Advertisement
I hope someone can help me with this...I've done a lot of searching but haven't found anything that addresses it.

I have a JList of objects, and I want to be able to right-click one of them and have a menu come up of things that I can do to that object (edit, delete, etc.). I'm getting the menu to come up okay, but it doesn't select the object. Instead, I have to first select it by left-clicking, then right-click to bring up the menu.

I'm sure there's a simple way to do it, but I can't seem to find it or figure it out.

Any help would be greatly appreciated.


Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Re: How Do I Do Right-Click Selection? Posted: Mar 10, 2005 5:19 AM
Reply to this message Reply
Never mind, I've got it now. I don't know how I overlooked the locationToIndex() method on JList when I went into the API the other day specifically looking for something like that, but somehow it slipped past me. Once I spotted it this morning, everything fell into place. In case anyone else is interested...

yourList.setSelectedIndex(yourList.locationToIndex(new Point(mouseEvent.getX(), mouseEvent.getY())));


Nothing to it, really.

bob

Posts: 3
Nickname: plmm
Registered: Mar, 2005

Re: How Do I Do Right-Click Selection? Posted: Mar 11, 2005 3:56 AM
Reply to this message Reply
yeahhhhhhhhhhhhhhhhhhhh m888888888888888888888888888888888

Jez Nicholson

Posts: 2
Nickname: jnicho02
Registered: Jun, 2005

mouseEvent.getPoint() Posted: Jun 1, 2005 2:59 AM
Reply to this message Reply
Thanks Ken. I too just wasted about 1/2 hour trying to do this. The BasicListUI definitely doesn't want to react to right-click as in it's MouseInputHandler.adjustFocusAndSelection(mouseEvent) it has:

if (!SwingUtilities.isLeftMouseButton(e)) {
return;
}


Remember that mouseEvent has a getPoint() method, so you code could be even shorter...

yourList.setSelectedIndex(yourList.locationToIndex(mouseEvent.getP oint()));

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: How Do I Do Right-Click Selection? Posted: Jun 1, 2005 4:01 AM
Reply to this message Reply
Thank you, thank you, thank you, thank you, thank you.


I'd spend some time looking into this before I gave up.

Flat View: This topic has 4 replies on 1 page
Topic: convert java to pseudocode Previous Topic   Next Topic Topic: tomcat is not startup when clickinon startup.bat

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use