Boomah
Boomah

Reputation: 1192

How do you stop focus going to the tab when clicking in the tabs content?

The program below shows a button and a label in a tab. The button has focus on start up. If you click anywhere in the content area apart from on the button, the button loses focus and the tab gains focus.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class FocusTest  extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        TabPane tabPane = new TabPane();
        tabPane.setFocusTraversable(false);
        Scene scene = new Scene(tabPane, 500, 500);
        stage.setScene(scene);
        Tab tab = new Tab("Tab 1");
        FlowPane contentPane = new FlowPane();
        /*contentPane.setOnMousePressed(MouseEvent::consume);*/
        tab.setContent(contentPane);
        tabPane.getTabs().addAll(tab, new Tab("Tab 2"));
        Button button = new Button("Button");
        Label label = new Label("Label");
        contentPane.getChildren().addAll(button, label);
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

I don't think the button should lose focus when clicking in the tab's content area. I still want to be able to focus the tab by clicking on the tab's name/text bit and use the shortcut keys to change tab.

I couldn't find a way to do it using the Tab or the TabPane but if I add a mouse pressed listener on the content pane (see commented out code above) I do get the behaviour I require but it feels like a bit of a hack. And I would have to set a listener on every item in tab pane.

So I suppose my question is, is there a better way to do this?


I raised an issue for this:

https://javafx-jira.kenai.com/browse/RT-37941

Thanks

Upvotes: 2

Views: 1456

Answers (1)

sazzy4o
sazzy4o

Reputation: 3343

You can request focus to the Button when the contentPane (FlowPane) is clicked here is the modified version of your code:

    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.FlowPane;
    import javafx.stage.Stage;

    public class FocusTest  extends Application {
@Override
public void start(Stage stage) throws Exception {
    TabPane tabPane = new TabPane();
    tabPane.setFocusTraversable(false);
    Scene scene = new Scene(tabPane, 500, 500);
    stage.setScene(scene);
    final Button button = new Button("Button");// I moved this here and added final
    Tab tab = new Tab("Tab 1");
    FlowPane contentPane = new FlowPane();
    contentPane.setOnMouseClicked(new EventHandler<MouseEvent>() {//I think this is what you are looking for
        public void handle(MouseEvent event) {
            button.requestFocus();
        }
    });                          
    tab.setContent(contentPane);
    tabPane.getTabs().addAll(tab, new Tab("Tab 2"));

    Label label = new Label("Label");
    contentPane.getChildren().addAll(button, label);
    stage.show();
}

public static void main(String[] args) {
    Application.launch(args);
}
}

If you have any question or would like to know how to do the same thing with multiple buttons or other object comment
I hope this will help you!

Upvotes: 1

Related Questions