Re: How to detect double-click mouse events in Swing?



Wes Harrison wrote:
I think my understanding of mouse clicked events is wrong because I don't
get the behaviour I expect. When I implement the following code:

table.getTableHeader().addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent event)
{
System.out.println("Mouse event, clickCount = " +
event.getClickCount());
}
}

I get the following output for every double-click:

Mouse event, clickCount = 1
Mouse event, clickCount = 2

Shouldn't it be just one event with a clickCount of 2? How can I tell the
difference between the first event and a normal single-click?

I am using Java 6.0 b89 but it probably applies to Java 5.0 and earlier as
well.

Thanks,

Wes

Have you considered SWT? It is able to implement a doubleClick method
since it uses OS specific libraries for UI. (Which makes it fast.)

Otherwise seems like a small reason to switch GUI's if you are taking
advantage of the richness of Swing.

An alternative would be to create a short duration javax.swing.timer
equal to a double click delay that you decide on (or let the user
configure separate from their OS value). On a first click of the mouse
event on the target widget start the timer. If a second mouse click
comes in on the same widget before the timer expires, stop the timer
and do your double click operation. If the timer expires before a
second mouse click, stop the timer and do your single click operation
(in the timer's ActionListener's actionPerformed method).

.



Relevant Pages

  • Re: Windows Service stopped working
    ... between the start of the service to 2AM, and sets the timer. ... timer expires, it re-reads the configuration file ... Dim MyLog As New EventLog ...
    (microsoft.public.dotnet.languages.vb)
  • termios VMIN and VTIME behavior
    ... instead of waiting for either the timer to expire or VMIN bytes. ... In non-canonical mode input processing, ... TIME serves as an inter-byte timer which shall be activated ... If the timer expires before MIN ...
    (Linux-Kernel)
  • [PATCH v4 1/3] ftrace: add tracepoint for timer
    ... This patch is modify from Mathieu's patch base on Ingo's suggestion, ... We expect the timer expires at 4294813629, after 199 jiffies, actually the ...
    (Linux-Kernel)
  • Re: [PATCH 1/3] ftrace: add tracepoint for timer
    ... We can detect a timer's delay if jiffies info is added, ... We expect the timer expires at 4294941565, ...
    (Linux-Kernel)
  • Re: Threads and Timing
    ... "Samer Saghir" wrote in message ... >>I tried using System.Threading.Timer and the period does the repeating ... I.e. when the timer expires, ...
    (microsoft.public.dotnet.framework.compactframework)