Question Details

No question body available.

Tags

java javafx

Answers (3)

March 31, 2025 Score: 3 Rep: 10,699 Quality: Medium Completeness: 100%

I agree with the comments mentioned by @JamesD regarding predicting for next click. By default the single click mouse pressed will do the selection or get to edit mode. If you are desperate to fix this problem, you need to separate the event processing of double click from single click.

You can achieve this by creating some custom events and a custom event dispatcher that can process the events separately.

The below solution involved four steps:

Step#1: Create a custom event and event dispatcher

You can create a custom double pressed event and event dispatcher to separate the events. The general idea is,

  • when we receive a single click event, we start a timeline to execute the event after certain duration.
  • If we receive another event within this duration, we consider this as a double pressed event, and will fire the custom double pressed event and cancel the previous single pressed event. That way you can separate the two events and handle them separately.

The code will be as below. In

/
 * Custom double pressed mouse event.
 */
interface CustomMouseEvent {
    EventType MOUSEDOUBLEPRESSED = new EventType(MouseEvent.ANY, "MOUSEDOUBLEPRESSED");
}

/
* Custom EventDispatcher to differentiate from double click and single click. */ class DoubleClickEventDispatcher implements EventDispatcher {

/ Default delay to fire a double click event in milliseconds. */ private static final long DEFAULT
DOUBLECLICKDELAY = 215;

/
Default event dispatcher of a node. / private final EventDispatcher defaultEventDispatcher;

/ Timeline for dispatching mouse clicked event.
/ private Timeline singleClickTimeline;

/
Constructor. @param initial Default event dispatcher of a node / public DoubleClickEventDispatcher(final EventDispatcher initial) { defaultEventDispatcher = initial; }

@Override public Event dispatchEvent(final Event event, final EventDispatchChain tail) { final EventType
March 30, 2025 Score: 1 Rep: 211,055 Quality: Medium Completeness: 60%

This answer comes with a big caveat: modifying the behavior of JavaFX controls is really not well supported. Doing so typically relies on undocumented implementation details of the control, which are subject to change in later releases. Hence implementing any such functionality is really not very robust.

That said, the current table cell implementation appears to manage selection and editing via listeners for mouse pressed and released events. So consuming a mouse pressed event (note: mouse pressed, not mouse clicked) in an event filter (note: filter not handler, so it is invoked before the cell's internal handlers) will prevent the default selection and editing behavior. The following (as a replacement for your existing row.setOnMouseClicked(...)) seems to result in the functionality you specify:

row.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> { if (event.getClickCount() == 2 && !row.isEmpty()) { Person person = row.getItem(); System.out.println("DoubleClick on " + person.firstNameProperty().get() + " " + person.lastNameProperty().get()); event.consume(); } });
March 31, 2025 Score: 1 Rep: 7,990 Quality: Low Completeness: 80%

I would record the time of the first click in a variable. Then, compare it to the time of the next click before deciding what to do. If the second click is within X milliseconds, then consider it a "double-click" and execute the special action. Else, consider the second click the trigger for entering edit mode.

OK, what happens if someone clicks a field, which selects it, and then double-clicks? With this scenario, it would go into Edit mode and then the second click of the double would do whatever a click does when editing that field.

If that is a problem, one possibility is to set the "edit" click to wait X millis before going to edit mode, giving time for a possible "double click" to assert itself.

I've done this sort of thing with Java, using Timer, and JavaScript, using setTimeout. I can't recall if JavaFX has a preferred timer.

For similar logic, check the topic of "debouncing". A quick search gave me a JS example here What is debouncing? but I'm only seeing a couple articles in Medium that show debouncing using Java or JavaFX. I haven't clicked on them because I am conserving my Medium freebies.