Reputation: 1
Please have a look at the following sample code. Start the application and focus one of the text fields. Now click the menu. The focus is still kept on the TextField. However our TextField triggers a communication of it's value on focus loss. How do I implement that clicking on a menu or toolbar item triggers a focus loss on the TextField?
public class FocusApp extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
TextField l_text1 = new TextField("TextField 1");
TextField l_text2 = new TextField("TextField 2");
l_text1.focusedProperty().addListener((a,b,c) -> {
System.out.println("Focus change on TextField 1");
});
l_text2.focusedProperty().addListener((a,b,c) -> {
System.out.println("Focus change on TextField 2");
});
MenuBar l_menuBar = new MenuBar();
l_menuBar.setFocusTraversable(true);
Menu l_menu = new Menu("_Menu");
l_menu.getItems().add(new MenuItem("_Item"));
l_menuBar.getMenus().add(l_menu);
VBox l_vbox = new VBox(l_menuBar, l_text1, l_text2);
primaryStage.setScene(new Scene(l_vbox));
primaryStage.show();
}
public static void main(String[] args)
{
FocusApp.launch(args);
}
}
Upvotes: 0
Views: 691
Reputation: 209694
I think you are asking that you want the focus to move away from the text field when the user activates the menu.
You could do this with
l_menu.setOnShowing(e -> l_menuBar.requestFocus());
Upvotes: 2