Adds a event handler to the given JavaFX node. - Java JavaFX

Java examples for JavaFX:Action

Description

Adds a event handler to the given JavaFX node.

Demo Code


    //package com.java2s;
    import javafx.scene.Node;
    import javafx.scene.control.ButtonBase;

    import javafx.scene.control.TextField;

    public class Main {
        /**//from www .  ja va 2 s.com
         * Adds a event handler to the given node. If the node extends ButtonBase or Textfield a action listener will be
         * used. Otherwise the action will be triggered on doubleclick
         *
         * @param node The node that should trigger the action
         * @param action the action
         */
        public static void defineNodeAction(Node node, Runnable action) {
            if (node instanceof ButtonBase) {
                ((ButtonBase) node).setOnAction((e) -> action.run());
            } else if (node instanceof TextField) {
                ((TextField) node).setOnAction((e) -> action.run());
            } else {
                node.setOnMouseClicked((e) -> {
                    if (e.getClickCount() > 1) {
                        action.run();
                    }
                });
            }
        }
    }

Related Tutorials