0

How to requestFocus on TextField of different Tabs of TabPane, whenever the Enter Key is pressed on that particular Tab.

Each Tab within TabPane contains TextField, whenever a particular Tab is selected using Enter Key, than the TextField within that Tab must get Focused (requestFocus)

package tabdemo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class TabDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        TabPane tabPane = new TabPane();
        tabPane.setPrefHeight(500);
        VBox.setVgrow(tabPane, Priority.ALWAYS);
        tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
        tabPane.setStyle("-fx-background-color: #fff; -fx-border-color: #e7eaec #e7eaec #e7eaec #e7eaec; -fx-border-width: 6;");

        Tab tab1 = new Tab();
        tab1.setText(" Try Demo");
        tab1.setId("TryDemo");
        TextField username1 = new TextField();
        tab1.setContent(username1);

        Tab tab2 = new Tab();
        tab2.setText(" Select Company");
        tab2.setId("SelectCompany");
        TextField username2 = new TextField();
        tab2.setContent(username2);

        Tab tab3 = new Tab();
        tab3.setText(" Create Company");
        tab3.setId("CreateCompany");
        TextField username3 = new TextField();
        tab3.setContent(username3);

        //Add childen to tabpane
        tabPane.getTabs().addAll(tab1, tab2, tab3);

        StackPane root = new StackPane();
        root.getChildren().add(tabPane);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

        // Tabpane Change Listener
        tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {

            @Override
            public void changed(ObservableValue<? extends Tab> arg0, Tab oldtab, Tab newtab) {
                if (newtab == tab1) {

                }
                if (newtab == tab2) {

                }
                if (newtab == tab3) {
                    //tab3.getContent().requestFocus();
                    //username3.requestFocus();
                }
            }
        });
        // End Tabpane Change Listener

        // Root Listner
        root.setOnKeyPressed(new EventHandler<KeyEvent>() {

            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.ENTER) {
                    tabPane.requestFocus();
                    event.consume();
                }
            }
        });
        // END Root Listner

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Any help would be appreciated.

Vikas Singh
  • 175
  • 5
  • 19

1 Answers1

1

Change your implementation of the ChangeListener to this:

// Tabpane Change Listener
tabPane.getSelectionModel().selectedItemProperty().addListener((arg0, oldtab, newtab) -> {
    TextField field = (TextField) newtab.getContent();
    Platform.runLater(() -> field.requestFocus());
});

This ensures that the request for the focus is pushed as the last 'task' for the application thread.

See also RequestFocus in TextField doesn't work -JavaFX 2.1 and JavaFX: Focusing textfield programmatically

Community
  • 1
  • 1
hotzst
  • 7,238
  • 9
  • 41
  • 64