JavaFX Check if multiple elements are visible

I encoded an animation that removes the search bar from my navigation bar when a user clicks on the Users tab.

I want the reverse animation to play if my search bar loses focus or if the navigation button that the user clicked to move the bar loses focus (as described in Figure 1) image describing my problem

I am currently achieving an effect by setting the onMouseExited property of search_wrapper to run my hideUsers() method, but ideally I want to check

 nav_button.setOnMouseExited(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent e) { if(search_wrapper loses focus OR nav_button loses focus) hideUsers(); } } 

How can i do this? I am tired of using the .isFocused () method for both elements, but gave nothing.

Any feedback is welcome.

+6
source share
1 answer

Why isFocused() not working?
In any case, I can suggest you try using the JavaFX property binding functions:

 // define a boolean property final BooleanProperty multipleFocusedProperty = new SimpleBooleanProperty(); // add listener to track changes in its value, for debugging purpose only here multipleFocusedProperty.addListener(new ChangeListener<Boolean>() { @Override public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) { System.out.println("newValue multiple focused = " + newValue); } }); // bind it to other property values multipleFocusedProperty.bind(search_wrapper.focusedProperty().not().or(nav_button.focusedProperty().not())); // finally use it as nav_button.setOnMouseExited(new EventHandler<MouseEvent>() { if(multipleFocusedProperty.getValue()) hideUsers(); } 
+1
source

All Articles