The push and hold event can be easily modeled in JavaFX, as in this question .
, , , ContextMenu TextField. TextField.getContextMenu() , , .
, TextFieldBehavior. public void contextMenuRequested(ContextMenuEvent e);, , , ContextMenuEvent TextField.
:
public class BasicView extends View {
public BasicView(String name) {
super(name);
TextField textField = new TextField();
addPressAndHoldHandler(textField, Duration.seconds(1), event -> {
Bounds bounds = textField.localToScreen(textField.getBoundsInLocal());
textField.fireEvent(new ContextMenuEvent(ContextMenuEvent.CONTEXT_MENU_REQUESTED,
0, 0, bounds.getMinX() + 10, bounds.getMaxY() + 10, false, null));
});
setCenter(new VBox(15.0, new Label("Push and hold for ContextMenu"), textField));
}
private void addPressAndHoldHandler(Node node, Duration holdTime, EventHandler<MouseEvent> handler) {
class Wrapper<T> {
T content;
}
Wrapper<MouseEvent> eventWrapper = new Wrapper<>();
PauseTransition holdTimer = new PauseTransition(holdTime);
holdTimer.setOnFinished(event -> handler.handle(eventWrapper.content));
node.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {
eventWrapper.content = event;
holdTimer.playFromStart();
});
node.addEventHandler(MouseEvent.MOUSE_RELEASED, event -> holdTimer.stop());
node.addEventHandler(MouseEvent.DRAG_DETECTED, event -> holdTimer.stop());
}
@Override
protected void updateAppBar(AppBar appBar) {
appBar.setTitleText("Push and Hold");
}
}
, :

, ContextMenu Android, JavaFX :

, , .