Java Code Examples for javafx.scene.Node#addEventHandler()

The following examples show how to use javafx.scene.Node#addEventHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChartPlugin.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <T extends InputEvent> void addEventHandlers(final Node node) {
    if (node == null) {
        return;
    }
    for (final Pair<EventType<? extends InputEvent>, EventHandler<? extends InputEvent>> pair : mouseEventHandlers) {
        final EventType<T> type = (EventType<T>) pair.getKey();
        final EventHandler<T> handler = (EventHandler<T>) pair.getValue();
        node.addEventHandler(type, handler);
        node.sceneProperty().addListener((ch, o, n) -> {
            if (o == n) {
                return;
            }
            if (o != null) {
                o.removeEventHandler(type, handler);
            }

            if (n != null) {
                n.addEventHandler(type, handler);
            }
        });
    }
}
 
Example 2
Source File: JFXNodesList.java    From JFoenix with Apache License 2.0 6 votes vote down vote up
private void initChild(Node node, int index, BiFunction<Boolean, Duration, Collection<KeyFrame>> animationFramesFunction, boolean addTriggerListener) {
    if (index > 0) {
        initNode(node);
        node.setVisible(false);
    } else {
        if (addTriggerListener) {
            if (node instanceof Button) {
                node.addEventHandler(ActionEvent.ACTION, event -> animateList());
            } else {
                node.addEventHandler(MouseEvent.MOUSE_CLICKED, event-> animateList());
            }
        }
        node.getStyleClass().add("trigger-node");
        node.setVisible(true);
    }

    if (animationFramesFunction == null && index != 0) {
        animationFramesFunction = initDefaultAnimation(node);
    } else if (animationFramesFunction == null && index == 0) {
        animationFramesFunction = (aBoolean, duration) -> new ArrayList<>();
    }

    animationsMap.put(node, animationFramesFunction);
}
 
Example 3
Source File: KeyMap.java    From FxDock with Apache License 2.0 6 votes vote down vote up
protected static KeyMap get(Node n)
{
	Object x = n.getProperties().get(KEY);
	if(x instanceof KeyMap)
	{
		return (KeyMap)x;
	}
	
	KeyMap m = new KeyMap();
	n.getProperties().put(KEY, m);

	n.addEventHandler(KeyEvent.KEY_PRESSED, (ev) -> m.handleEvent(ev));
	n.addEventHandler(KeyEvent.KEY_RELEASED, (ev) -> m.handleEvent(ev));
	n.addEventHandler(KeyEvent.KEY_TYPED, (ev) -> m.handleEvent(ev));
	return m;
}
 
Example 4
Source File: FX.java    From FxDock with Apache License 2.0 6 votes vote down vote up
public static void onKey(Node node, KeyCode code, FxAction a)
{
	node.addEventHandler(KeyEvent.KEY_PRESSED, (ev) ->
	{
		if(ev.getCode() == code)
		{
			if(ev.isAltDown() || ev.isControlDown() || ev.isMetaDown() || ev.isShiftDown() || ev.isShortcutDown())
			{
				return;
			}
			else
			{
				a.invokeAction();
				ev.consume();
			}
		}
	});
}
 
Example 5
Source File: StyledTextFXControlAdapterSnippet.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
private void styleAction(Node actionNode,
		final FXControlAdapter<StyledText> stNode, final int fontStyle,
		final boolean underline) {
	actionNode.addEventHandler(ActionEvent.ACTION,
			new EventHandler<ActionEvent>() {
				@Override
				public void handle(ActionEvent event) {
					StyledText st = stNode.getControl();
					if (st.getSelectionText().equals("")) {
						return;
					}
					Point selRange = st.getSelectionRange();
					StyleRange styleRange = new StyleRange();
					styleRange.start = selRange.x;
					styleRange.length = selRange.y;
					styleRange.fontStyle = fontStyle;
					styleRange.underline = underline;
					st.setStyleRange(styleRange);
				}
			});
}
 
Example 6
Source File: CellListManager.java    From Flowless with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private C cellForItem(T item) {
    C cell = cellPool.getCell(item);

    // apply CSS when the cell is first added to the scene
    Node node = cell.getNode();
    EventStreams.nonNullValuesOf(node.sceneProperty())
            .subscribeForOne(scene -> {
                node.applyCss();
            });

    // Make cell initially invisible.
    // It will be made visible when it is positioned.
    node.setVisible(false);

    if (cell.isReusable()) {
        // if cell is reused i think adding event handler
        // would cause resource leakage.
        node.setOnScroll(this::pushScrollEvent);
        node.setOnScrollStarted(this::pushScrollEvent);
        node.setOnScrollFinished(this::pushScrollEvent);
    } else {
        node.addEventHandler(ScrollEvent.ANY, this::pushScrollEvent);
    }

    return cell;
}
 
Example 7
Source File: JavaFxUtils.java    From milkman with MIT License 5 votes vote down vote up
public static void publishEscToParent(Node node){
    node.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
        //propagate to parent so that dialog closes correctly / request actually stops
        if (e.getCode() == KeyCode.ESCAPE){
            var newEvt = e.copyFor(e.getSource(), node.getParent());
            node.getParent().fireEvent(newEvt);
        }
    });
}
 
Example 8
Source File: CrewEditorFX.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Swaps the mouse cursor type between DEFAULT and HAND
 *
 * @param node
 */
public void setMouseCursor(Node node) {
	node.addEventHandler(MouseEvent.MOUSE_EXITED, event -> {
		node.setCursor(Cursor.DEFAULT);
	});

	node.addEventHandler(MouseEvent.MOUSE_ENTERED, event -> {
		node.setCursor(Cursor.HAND);
	});
}
 
Example 9
Source File: ScenarioConfigEditorFX.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Swaps the mouse cursor type between DEFAULT and HAND
 * 
 * @param node
 */
public void setMouseCursor(Node node) {
	node.addEventHandler(MouseEvent.MOUSE_EXITED, event -> {
		node.setCursor(Cursor.DEFAULT);
	});

	node.addEventHandler(MouseEvent.MOUSE_ENTERED, event -> {
		node.setCursor(Cursor.HAND);
	});
}
 
Example 10
Source File: ConnectorDragManager.java    From graph-editor with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Adds mouse-drag handlers to a particular connector.
 *
 * @param connector the {@link GConnector} to which mouse-drag handlers should be added
 */
private void addMouseDragHandlers(final GConnector connector) {

    final EventHandler<MouseEvent> newDragDetectedHandler = event -> handleDragDetected(event, connector);
    final EventHandler<MouseEvent> newMouseDraggedHandler = event -> handleMouseDragged(event, connector);
    final EventHandler<MouseDragEvent> newMouseDragEnteredHandler = event -> handleDragEntered(event, connector);
    final EventHandler<MouseDragEvent> newMouseDragExitedHandler = event -> handleDragExited(event, connector);
    final EventHandler<MouseDragEvent> newMouseDragReleasedHandler = event -> handleDragReleased(event, connector);

    final Node root = skinLookup.lookupConnector(connector).getRoot();

    root.addEventHandler(MouseEvent.DRAG_DETECTED, newDragDetectedHandler);
    root.addEventHandler(MouseEvent.MOUSE_DRAGGED, newMouseDraggedHandler);
    root.addEventHandler(MouseDragEvent.MOUSE_DRAG_ENTERED, newMouseDragEnteredHandler);
    root.addEventHandler(MouseDragEvent.MOUSE_DRAG_EXITED, newMouseDragExitedHandler);
    root.addEventHandler(MouseDragEvent.MOUSE_DRAG_RELEASED, newMouseDragReleasedHandler);
}
 
Example 11
Source File: ConnectorDragManager.java    From graph-editor with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Adds mouse handlers to a particular connector.
 *
 * @param connector the {@link GConnector} to which mouse handlers should be added
 */
private void addMouseHandlers(final GConnector connector) {

    final EventHandler<MouseEvent> newMouseEnteredHandler = event -> handleMouseEntered(event, connector);
    final EventHandler<MouseEvent> newMouseExitedHandler = event -> handleMouseExited(event, connector);
    final EventHandler<MouseEvent> newMouseReleasedHandler = event -> handleMouseReleased(event, connector);
    final EventHandler<MouseEvent> newMouseMovedHandler = event -> event.consume();

    final Node root = skinLookup.lookupConnector(connector).getRoot();

    root.addEventHandler(MouseEvent.MOUSE_ENTERED, newMouseEnteredHandler);
    root.addEventHandler(MouseEvent.MOUSE_EXITED, newMouseExitedHandler);
    root.addEventHandler(MouseEvent.MOUSE_RELEASED, newMouseReleasedHandler);
    root.addEventHandler(MouseEvent.MOUSE_MOVED, newMouseMovedHandler);
}
 
Example 12
Source File: Rubberband.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
/** Create rubber
 *  @param event_source       Node that will react to mouse click/drag/release,
 *                            where the user will be able to 'start' a rubber band selection
 *  @param parent             Parent in which rubber band is displayed
 *  @param rubberband_handler Handler that will be invoked with the selected region
 *
 */
public Rubberband(final Node event_source, final Group parent, final RubberbandHandler rubberband_handler)
{
    this.parent = parent;
    this.handler = rubberband_handler;
    event_source.addEventHandler(MouseEvent.MOUSE_PRESSED, this::handleStart);
    event_source.addEventFilter(MouseEvent.MOUSE_DRAGGED, this::handleDrag);
    event_source.addEventFilter(MouseEvent.MOUSE_RELEASED, this::handleStop);
    rect = new Rectangle(0, 0, 0, 0);
    rect.setArcWidth(5);
    rect.setArcHeight(5);
    rect.getStyleClass().add("rubberband");
}
 
Example 13
Source File: JFXNodeUtils.java    From JFoenix with Apache License 2.0 5 votes vote down vote up
public static void addPressAndHoldHandler(Node node, Duration holdTime,
                                          EventHandler<MouseEvent> handler) {
    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());
}
 
Example 14
Source File: CellUtils.java    From ARMStrong with Mozilla Public License 2.0 5 votes vote down vote up
private static <T> boolean listenToComboBoxSkin(final ComboBox<T> comboBox, final Cell<T> cell) {
    Skin<?> skin = comboBox.getSkin();
    if (skin != null && skin instanceof ComboBoxListViewSkin) {
        ComboBoxListViewSkin cbSkin = (ComboBoxListViewSkin) skin;
        Node popupContent = cbSkin.getPopupContent();
        if (popupContent != null && popupContent instanceof ListView) {
            popupContent.addEventHandler(MouseEvent.MOUSE_RELEASED, e -> cell.commitEdit(comboBox.getValue()));
            return true;
        }
    }
    return false;
}
 
Example 15
Source File: ResizeHelper.java    From JavaFX-Chat with GNU General Public License v3.0 5 votes vote down vote up
public static void addListenerDeeply(Node node, EventHandler<MouseEvent> listener) {
    node.addEventHandler(MouseEvent.MOUSE_MOVED, listener);
    node.addEventHandler(MouseEvent.MOUSE_PRESSED, listener);
    node.addEventHandler(MouseEvent.MOUSE_DRAGGED, listener);
    node.addEventHandler(MouseEvent.MOUSE_EXITED, listener);
    node.addEventHandler(MouseEvent.MOUSE_EXITED_TARGET, listener);
    if (node instanceof Parent) {
        Parent parent = (Parent) node;
        ObservableList<Node> children = parent.getChildrenUnmodifiable();
        for (Node child : children) {
            addListenerDeeply(child, listener);
        }
    }
}
 
Example 16
Source File: ResizeHelper.java    From Path-of-Leveling with MIT License 5 votes vote down vote up
private static void addListenerDeeply(Node node, EventHandler<MouseEvent> listener) {
    node.addEventHandler(MouseEvent.MOUSE_MOVED, listener);
    node.addEventHandler(MouseEvent.MOUSE_PRESSED, listener);
    node.addEventHandler(MouseEvent.MOUSE_DRAGGED, listener);
    node.addEventHandler(MouseEvent.MOUSE_EXITED, listener);
    node.addEventHandler(MouseEvent.MOUSE_EXITED_TARGET, listener);
    if (node instanceof Parent) {
        Parent parent = (Parent) node;
        ObservableList<Node> children = parent.getChildrenUnmodifiable();
        for (Node child : children) {
            addListenerDeeply(child, listener);
        }
    }
}
 
Example 17
Source File: EventStreams.java    From ReactFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static <T extends Event> EventStream<T> eventsOf(
        Node node, EventType<T> eventType) {
    return new EventStreamBase<T>() {
        @Override
        protected Subscription observeInputs() {
            EventHandler<T> handler = this::emit;
            node.addEventHandler(eventType, handler);
            return () -> node.removeEventHandler(eventType, handler);
        }
    };
}
 
Example 18
Source File: SelectionCreator.java    From graph-editor with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Adds a click selection mechanism for nodes.
 */
private void addClickSelectionForNodes() {

    for (final GNode node : model.getNodes()) {

        final Region nodeRegion = skinLookup.lookupNode(node).getRoot();

        final EventHandler<MouseEvent> oldNodePressedHandler = nodePressedHandlers.get(node);
        final EventHandler<MouseEvent> oldNodeReleasedHandler = nodeReleasedHandlers.get(node);

        if (oldNodePressedHandler != null) {
            nodeRegion.removeEventHandler(MouseEvent.MOUSE_PRESSED, oldNodePressedHandler);
        }

        if (oldNodeReleasedHandler != null) {
            nodeRegion.removeEventHandler(MouseEvent.MOUSE_RELEASED, oldNodeReleasedHandler);
        }

        final EventHandler<MouseEvent> newNodePressedHandler = event -> handleNodePressed(event, node);
        final EventHandler<MouseEvent> newNodeReleasedHandler = event -> handleNodeReleased(event, node);

        nodeRegion.addEventHandler(MouseEvent.MOUSE_PRESSED, newNodePressedHandler);
        nodeRegion.addEventHandler(MouseEvent.MOUSE_RELEASED, newNodeReleasedHandler);

        nodePressedHandlers.put(node, newNodePressedHandler);
        nodeReleasedHandlers.put(node, newNodeReleasedHandler);

        for (final GConnector connector : node.getConnectors()) {

            final Node connectorRoot = skinLookup.lookupConnector(connector).getRoot();

            final EventHandler<MouseEvent> oldConnectorPressedHandler = connectorPressedHandlers.get(connector);

            if (oldConnectorPressedHandler != null) {
                connectorRoot.removeEventHandler(MouseEvent.MOUSE_PRESSED, oldConnectorPressedHandler);
            }

            final EventHandler<MouseEvent> newConnectorPressedHandler = event -> handleConnectorPressed(event);

            connectorRoot.addEventHandler(MouseEvent.MOUSE_PRESSED, newConnectorPressedHandler);
            connectorPressedHandlers.put(connector, newConnectorPressedHandler);
        }
    }
}
 
Example 19
Source File: DragSupport.java    From scenic-view with GNU General Public License v3.0 4 votes vote down vote up
public DragSupport(Node target, final KeyCode modifier, final MouseButton mouseButton, final Orientation orientation, final Property<Number> property, final double factor) {
    this.target = target;
    mouseEventHandler = new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent t) {
            if (t.getEventType() != MouseEvent.MOUSE_ENTERED_TARGET
                    && t.getEventType() != MouseEvent.MOUSE_EXITED_TARGET) {
                lastMouseEvent = t;
            }
            if (t.getEventType() == MouseEvent.MOUSE_PRESSED) {
                if (t.getButton() == mouseButton
                        && isModifierCorrect(t, modifier)) {
                    anchor = property.getValue();
                    dragAnchor = getCoord(t, orientation);
                    t.consume();
                }
            } else if (t.getEventType() == MouseEvent.MOUSE_DRAGGED) {
                if (t.getButton() == mouseButton
                        && isModifierCorrect(t, modifier)) {
                    property.setValue(anchor.doubleValue()
                            + (getCoord(t, orientation) - dragAnchor) * factor);
                    t.consume();
                }
            }
        }
    };
    keyboardEventHandler = (KeyEvent t) -> {
        if (t.getEventType() == KeyEvent.KEY_PRESSED) {
            if (t.getCode() == modifier) {
                anchor = property.getValue();
                if (lastMouseEvent != null) {
                    dragAnchor = getCoord(lastMouseEvent, orientation);
                }
                t.consume();
            }
        } else if (t.getEventType() == KeyEvent.KEY_RELEASED) {
            if (t.getCode() != modifier && isModifierCorrect(t, modifier)) {
                anchor = property.getValue();
                if (lastMouseEvent != null) {
                    dragAnchor = getCoord(lastMouseEvent, orientation);
                }
                t.consume();
            }
        }
    };
    target.addEventHandler(MouseEvent.ANY, mouseEventHandler);
    target.addEventHandler(KeyEvent.ANY, keyboardEventHandler);
}
 
Example 20
Source File: JFXChartUtil.java    From jfxutils with Apache License 2.0 3 votes vote down vote up
/**
 * Adds a handler that sets X and/or Y axis auto ranging when the primary mouse button is double-clicked. If the click
 * occurs inside the X axis area auto range only the X axis and similarly for Y axis. But if the event is in the plot
 * area or anywhere else, auto range both axes.
 *
 * @param chart  chart whose axes to auto range
 * @param target handler listens for {@link MouseEvent}s on this {@link Node}
 *
 * @return the {@link EventHandler} added to 'target' for {@link MouseEvent}.
 */
public static EventHandler<MouseEvent> addDoublePrimaryClickAutoRangeHandler( XYChart<?, ?> chart, Node target ) {
	XYChartInfo chartInfo = new XYChartInfo( chart, target );
	EventHandler<MouseEvent> handler = getDoublePrimaryClickAutoRangeHandler( chartInfo );
	target.addEventHandler( MouseEvent.MOUSE_CLICKED, handler );
	return handler;
}