Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Check if multiple elements are being hovered

Tags:

java

javafx

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)image describing my problem

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.

like image 457
Halfpint Avatar asked Nov 11 '22 12:11

Halfpint


1 Answers

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();
}
like image 171
Uluk Biy Avatar answered Nov 15 '22 13:11

Uluk Biy