• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mouse Event - Single/ Double Click detection

 
Javier Vargas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello World!! ;o)

I am working in a UI with a JTree. I am implementing a MouseListener as recomended by JTree javaDocs (see the code below)

***************************************************************************
nal JTree tree = ...;

MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int selRow = tree.getRowForLocation(e.getX(), e.getY());
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
mySingleClick(selRow, selPath);
}
else if(e.getClickCount() == 2) {
myDoubleClick(selRow, selPath);
}
}
}
};
tree.addMouseListener(ml);
***************************************************************************

The problem I am having here is that, as the user will be a human(slower than the PC), with this code is almost impossible to detect if the user is doing a single click or a double click. That means, if the user is doing one click, there is non problem. But with double click, the above mentioned code detect the first click and go into "mySingleClick" an then detects the second one and go to the "myDoubleClick" method.

I want to know how can I do in order to detect the Double Click when de user is doing the double click?

Thanks in advance for your time, sincerely

javier
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an example on Sun site http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void mousePressed(MouseEvent e)


If you are checking for the 'click' event then this is not the appropriate method to rely on. You will have to write your code in mouseClicked method of MouseListener

Have a look at the MouseListener Tutorial for more details.
[ July 21, 2006: Message edited by: jaikiran pai ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!