JTable how to detect mouse clicks on row

Posted on 2004-03-29
Last Modified: 2012-08-13

How to detect double mouse click on JTable and get double clicked row index?

Tomas Vilda
Question by:beside
  • 3
  • 3
  • 2
  • +2
LVL 86

Expert Comment

ID: 10702989
LVL 37

Expert Comment

ID: 10702994

public class MyMouseAdapter extends MouseAdapter {
        public MyMouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if ( e.getSource().equals(yourTable) &&
                 SwingUtilities.isLeftMouseButton(e) && e.getClickCount()==2 )


combined with

        MyMouseAdapter listener = new MyMouseAdapter();
        theTable.addMouseListener( listener );

Expert Comment

ID: 10703015
Something like:

public void myTable_mouseClicked(MouseEvent e)
      if (e.getModifiers() == 16)  //left mousebutton
        if (e.getClickCount() == 2)  //double click

to get the index:


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)

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

LVL 30

Accepted Solution

Mayank S earned 250 total points
ID: 10703021
You can try something like this:

jTabBook.addMouseListener ( new MouseAdapter () {
      public void mousePressed ( MouseEvent e ) {
            if  ( e.getClickCount () == 2 )
                    Point origin = e.getPoint () ;
                    int row = jTabBook.rowAtPoint ( origin ) ;
                    int column = jTabBook.columnAtPoint ( origin ) ;

            if  ( row == -1 || column == -1 )
                      System.out.println ( "NO SELECTION DONE. " ) ; // end if
                      System.out.println ( row + " " + column ) ; // end else

            } // end if

      } // end of mousePressed ()
} ) ;
LVL 30

Expert Comment

by:Mayank S
ID: 10703031
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.

Expert Comment

ID: 10703041
>>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.
LVL 37

Expert Comment

ID: 10703048
>> 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. ;)

Author Comment

ID: 10703050
Very nice :) thanx mayankeagle, just copy pasted, and it works :)) nice one more time
LVL 37

Expert Comment

ID: 10703055
>> if (e.getModifiers() == 16)

Bwahh! Avoid magic numbers like that.
LVL 30

Expert Comment

by:Mayank S
ID: 10703063
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 ().

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 84
servlet example issue 6 38
expectj telnet failing 5 35
MySqlDump not dumping triggers 1 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now