Listen double clicks on JTable's row

hi,

I want to know how to catch double clicks did on a specific JTable's row, or any cell on that.

I know that's possible because JTable is a JComponent..so, I want to know how to catch the event when the user press Enter over a single selected row, too...

If you may show me and example or specific links, I'll thank you so much..

lucky..

[373 byte] By [gh0st_a] at [2007-11-26 19:28:00]
# 1

> I want to know how to catch double clicks did on a

> specific JTable's row, or any cell on that.

If you add a mouse listener to your table then in the

public void mouseClicked(MouseEvent e)

method you could do something like:

public void mouseClicked(MouseEvent e)

{

if (e.getClickCount() >= 2)

{

int row = activitiesTable.rowAtPoint(e.getPoint());

int column = activitiesTable.columnAtPoint(e.getPoint());

}

}

which gives you the row clicked on, the column clicked on and therefore the cell clicked on as well.

As for the other bit, i'm not sure, someone else will probbably be able to help.

Ruanaea at 2007-7-9 21:55:00 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thank u, Inmediatly I gonna try ur example
gh0st_a at 2007-7-9 21:55:00 > top of Java-index,Desktop,Core GUI APIs...
# 3

By default:

a) a double click invokes the editor for the cell, so the table won't get a double click as the second click is actually forwarded to the cell editor. So using a MouseListener will only work on non editable cells

b) The Enter key is used to move the row selection to the next row so you need to override the default mappings of the table.

This posting shows how you would do the above on a JList. The code should be similiar for a JTable.

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=626866

camickra at 2007-7-9 21:55:00 > top of Java-index,Desktop,Core GUI APIs...
# 4
It's work fine...thanks again
gh0st_a at 2007-7-9 21:55:00 > top of Java-index,Desktop,Core GUI APIs...
# 5

> b) The Enter key is used to move the row selection to

> the next row so you need to override the default

> mappings of the table.

>

> This posting shows how you would do the above on a

> JList. The code should be similiar for a JTable.

>

> http://forum.java.sun.com/thread.jspa?forumID=57&threa

> dID=626866

Thanks anyway...I fix that with a tricky and simplest solution.

I only put a KeyEventListener on the table and catch the keyEvent, and if it's a EnterKey, then check wich row is selected on table.

table.addkeyListener(new KeyListener(){

public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_ENTER){

int[] selected_rows = table.getSelectedRows();

if(selected_rows.length==1){

int row = selected_rows[0];

DefaultTableModel table_model = (DefaultTableModel)table.getModel();

Object element_at_row = directory_model.getValueAt(row, 1); //I wanna Only the element at column 1

//do more things...

}

}

}

}

//....other interface's methods..

);

gh0st_a at 2007-7-9 21:55:00 > top of Java-index,Desktop,Core GUI APIs...