I have coded an animation which slides out a search bar from my navigation bar when a user clicks on the 'Users' navigation tab.
I want the reverse animation to play if my searchbar loses focus or if the navigation button the user has pressed to to slide the bar out loses focus (as decribed in fig1)
I currently achieve the effect by setting the onMouseExited property of the 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 would I go about achieving this? I have tired using the .isFocused() method on both elements but that failed to produce any results.
Any feedback would be greatly appreciated.
Why is isFocused()
not working?
Anyway, I can suggest you to try using JavaFX's property binding features:
// 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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With