beside
asked on
JTable how to detect mouse clicks on row
Hello,
How to detect double mouse click on JTable and get double clicked row index?
Regards,
Tomas Vilda
How to detect double mouse click on JTable and get double clicked row index?
Regards,
Tomas Vilda
define
public class MyMouseAdapter extends MouseAdapter {
public MyMouseAdapter() {
}
public void mouseClicked(MouseEvent e) {
if ( e.getSource().equals(yourT able) &&
SwingUtilities.isLeftMouse Button(e) && e.getClickCount()==2 )
doDoubleClickAction(yourTa ble.rowAtP oint(e.get Point()));
}
}
combined with
MyMouseAdapter listener = new MyMouseAdapter();
theTable.addMouseListener( listener );
public class MyMouseAdapter extends MouseAdapter {
public MyMouseAdapter() {
}
public void mouseClicked(MouseEvent e) {
if ( e.getSource().equals(yourT
SwingUtilities.isLeftMouse
doDoubleClickAction(yourTa
}
}
combined with
MyMouseAdapter listener = new MyMouseAdapter();
theTable.addMouseListener(
Something like:
public void myTable_mouseClicked(Mouse Event e)
{
if (e.getModifiers() == 16) //left mousebutton
{
if (e.getClickCount() == 2) //double click
{
to get the index:
myTable.getSelectedRow();
Remember though that a double click event is always preceeded by a single click event, so you have to take that in consideration when writing the code (in other words, when you make a doubleclick, two mouse events will be fired)
/Nick
public void myTable_mouseClicked(Mouse
{
if (e.getModifiers() == 16) //left mousebutton
{
if (e.getClickCount() == 2) //double click
{
to get the index:
myTable.getSelectedRow();
Remember though that a double click event is always preceeded by a single click event, so you have to take that in consideration when writing the code (in other words, when you make a doubleclick, two mouse events will be fired)
/Nick
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
As far as I know, the double-click cannot be caught in the mouseClicked () method. It will give the click-count as 1 even in case of double-clicks. It has to be captured in the mousePressed () method.
>>As far as I know, the double-click cannot be caught in the mouseClicked () method
That's not true. This code is taken from an existing program, and it sure works there.
That's not true. This code is taken from an existing program, and it sure works there.
>> As far as I know, the double-click cannot be caught in the mouseClicked () method
I copied/pasted that code straight from my working app.
So, it *does* work. At least for me. ;)
I copied/pasted that code straight from my working app.
So, it *does* work. At least for me. ;)
ASKER
Very nice :) thanx mayankeagle, just copy pasted, and it works :)) nice one more time
>> if (e.getModifiers() == 16)
Bwahh! Avoid magic numbers like that.
Bwahh! Avoid magic numbers like that.
Well, when I had tried it, it did not work. What happened on a double-click (in order) was:
-> it goes into the mousePressed () method and gives click-count as 1
-> it goes again into the mousePressed () method and gives click-count as 2 - (A)
-> it goes into the mouseClicked () method and gives click-count as 1
So I guess the place to catch it is (A). I'm not sure if click-count always gives 2 in mouseClicked (). But it will definitely give 2 in mousePressed ().
-> it goes into the mousePressed () method and gives click-count as 1
-> it goes again into the mousePressed () method and gives click-count as 2 - (A)
-> it goes into the mouseClicked () method and gives click-count as 1
So I guess the place to catch it is (A). I'm not sure if click-count always gives 2 in mouseClicked (). But it will definitely give 2 in mousePressed ().
http://javaalmanac.com/egs/javax.swing.table/SelEvent.html