krish
krish

Reputation: 21

setFocusTraversable behavior on MenuBar in JavaFX 2.0

I have noticed an issue with setFocusTraversable() on MenuBar control. If I call setFocusTraversable(false) on menuBar object the focus traverses (I can see the menubar getting highlighted/selected) to menu when I press Tab from TextField but the event (changed()) does not get fired. If I call setFocusTraversable(true) on menuBar object and press Tab when in TextField the focus does not visually traverses to MenuBar(TextField loses focus) but the event gets fired and additionally the focus can not be set on TextField using Tab or Shift + Tab. I am not sure if this is a bug or problem with my understanding.

Here is the code.

public class MenuTest extends Application 
implements ChangeListener
{
    MenuBar menuBar = new MenuBar();
    TextField tf1 = new TextField("One");
    public static void main(String[] args)
    {
    Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage)
    {
        Group content = new Group();
        BorderPane paneLayout = new BorderPane();
        final Menu menu1 = new Menu("File");

        menuBar.getMenus().addAll(menu1);
        Menu exit = new Menu("Exit");
        menu1.getItems().add(exit);
        content.getChildren().add(tf1);
        paneLayout.setTop(menuBar);
        paneLayout.setCenter(content);
        Scene scene = new Scene(paneLayout, 300, 250);
        primaryStage.setScene(scene);
        primaryStage.show();

        menuBar.setFocusTraversable(false);
        menuBar.focusedProperty().addListener(this);
        tf1.focusedProperty().addListener(this);
        tf1.requestFocus();
    }

    public void changed(ObservableValue ov, Object t, Object t1)
    {
    System.out.println("focus gained - " + ov.toString()); 
    }
}

Please help.

Thanks, KK

PS: MenuBar API explicitly says that "MenuBar sets focusTraversable to false." but behaves differently.

Upvotes: 0

Views: 2310

Answers (1)

Sergey Grinev
Sergey Grinev

Reputation: 34528

Unfortunately you've met a bug: http://javafx-jira.kenai.com/browse/RT-20595

Upvotes: 1

Related Questions