Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2907
  • Last Modified:

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
0
beside
Asked:
beside
  • 3
  • 3
  • 2
  • +2
1 Solution
 
zzynxSoftware engineerCommented:
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
 
Nick_72Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
Nick_72Commented:
>>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
 
zzynxSoftware engineerCommented:
>> 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
 
besideAuthor Commented:
Very nice :) thanx mayankeagle, just copy pasted, and it works :)) nice one more time
0
 
zzynxSoftware engineerCommented:
>> if (e.getModifiers() == 16)

Bwahh! Avoid magic numbers like that.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now