Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JTable how to detect mouse clicks on row

Posted on 2004-03-29
10
Medium Priority
?
2,899 Views
Last Modified: 2012-08-13
Hello,

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

Regards,
Tomas Vilda
0
Comment
Question by:beside
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10702989
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10702994
define

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

}

combined with

        MyMouseAdapter listener = new MyMouseAdapter();
        theTable.addMouseListener( listener );
0
 
LVL 9

Expert Comment

by:Nick_72
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:

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
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 30

Accepted Solution

by:
Mayank S earned 1000 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
            else
                      System.out.println ( row + " " + column ) ; // end else

            } // end if

      } // end of mousePressed ()
} ) ;
0
 
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.
0
 
LVL 9

Expert Comment

by:Nick_72
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.
0
 
LVL 37

Expert Comment

by:zzynx
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. ;)
0
 

Author Comment

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

Expert Comment

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

Bwahh! Avoid magic numbers like that.
0
 
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 ().
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses

670 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