1

Is it possible to add .hoverProperty().addListener to all children(in my case buttons) of HBox? I know that I can assign separate listeners for each button. But I was interested if it is possible to assign one listener to all children at once. Buttons of HBox have 15 px spacing between them.

Alyona
  • 1,682
  • 2
  • 21
  • 44
  • Not really an answer to the question, but what do you want to do in the listener? If you are just changing the style of the button on hover, you can do that directly with CSS and not use a listener at all. – James_D Mar 02 '15 at 17:26

1 Answers1

3

Just add the listener to the HBox:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {

        Group root = new Group();

        HBox hBox = new HBox();
        hBox.setSpacing(30);

        for (int i = 0; i < 10; i++) {
            hBox.getChildren().add(new Button("Button " + i));
        }

        hBox.hoverProperty().addListener(new ChangeListener<Boolean>() {

            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                System.out.println("Hover: " + oldValue + " -> " + newValue);
            }
        });

        hBox.addEventFilter(MouseEvent.MOUSE_ENTERED, e -> System.out.println( e));
        hBox.addEventFilter(MouseEvent.MOUSE_EXITED, e -> System.out.println( e));
        hBox.addEventFilter(MouseEvent.MOUSE_MOVED, e -> {

            if( e.getTarget() instanceof Button) {
                System.out.println( e);
            }

        });

        hBox.setMaxHeight(100);

        root.getChildren().add( hBox);
        Scene scene = new Scene( root, 800, 500);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

According to the hoverProperty documentation you could as well use a mouse listener for now:

Note that current implementation of hover relies on mouse enter and exit events to determine whether this Node is in the hover state; this means that this feature is currently supported only on systems that have a mouse. Future implementations may provide alternative means of supporting hover.

Roland
  • 18,114
  • 12
  • 62
  • 93
  • My bad, I didn't mention that buttons in hbox have spacing between them. But hovered is firing even if user hovers those empty spaces. Is there a way to exclude spacing also being hovered? – Alyona Mar 02 '15 at 16:27
  • The simple solution is the one that's right in front of you. Add a listener to every button. Why don't you want that? The listener fires on enter and exit. So if you'd move Button -> Spacing -> Button, then the hover over the 2nd button would never trigger an event. An alternative would be to listen to the MOUSE_MOVED event and filter the target, i. e. check if it's a button or the hbox. But I don't know what you want to achieve. – Roland Mar 02 '15 at 16:57
  • I updated the code with the spacing and the mouse moved listener in case it helps you. – Roland Mar 02 '15 at 17:03