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)
  • 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)
  • Threads and Timing
    ... >I tried using System.Threading.Timer and the period does the repeating forme ... >2 - I have to have a seperate thread running to moniter the overall time, ... I.e. when the timer expires, ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Threads and Timing
    ... You should chain your timer events - do not use the period (use -1 instead ... I.e. when the timer expires, ... > I tried using System.Threading.Timer and the period does the repeating for ... > 2 - I have to have a seperate thread running to moniter the overall time, ...
    (microsoft.public.windowsce.app.development)