detect right mouse click on a row of JTable

Hello

I want to know the row number in a JTable when there is a right mouse click on it by using the code:

myTable.addMouseListener(new MouseAdapter(){

publicvoid mouseClicked(MouseEvent e){

if (e.getButton() == MouseEvent.BUTTON3){

int index = myTable.getSelectedRow();

});

But I alway get index = -1.

How I can fix this

Many thanks

shuhu

[678 byte] By [suhua] at [2007-11-27 10:52:23]
# 1

use this code instead

myTable.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e){

if (e.getButton() == MouseEvent.BUTTON3){

int index = myTable.rowAtPoint( e.getPoint() );

});

You can also use e.isPopupTrigger() instead of the MouseEvent.BUTTON3 comparison, since not all "mice" may have the three buttons

ICE

icewalker2ga at 2007-7-29 11:37:11 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for a very quick help

regards

suhua at 2007-7-29 11:37:11 > top of Java-index,Desktop,Core GUI APIs...
# 3

Hello

I replaced e.isPopupTrigger()

==================

myTable.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e){

if (e.isPopupTrigger()){

int index = myTable.rowAtPoint( e.getPoint() );

});

====================

However, it does not work

my computer is linux and three button mouse

Thanks

suhua at 2007-7-29 11:37:11 > top of Java-index,Desktop,Core GUI APIs...
# 4

On Linux, popup menus are triggered by MOUSE_PRESSED, not MOUSE_CLICKED. (On windows, by the way, it is MOUSE_RELEASED.)

Actually, this is kind of covered by the documentation for MouseEvent.isPopupTrigger():

"Note: Popup menus are triggered differently on different systems. Therefore, isPopupTrigger should be checked in both mousePressed and mouseReleased for proper cross-platform functionality."

Torgila at 2007-7-29 11:37:12 > top of Java-index,Desktop,Core GUI APIs...
# 5

you can also try swing utilities :)

myTable.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e){

if (javax.swing.SwingUtilities.isRightMouseButton(e)){

int index = myTable.rowAtPoint( e.getPoint() );

});

Yannixa at 2007-7-29 11:37:12 > top of Java-index,Desktop,Core GUI APIs...