Vikas Singh
Vikas Singh

Reputation: 165

Focus is lost from TextField whenever SHIFT key is Pressed (Javafx)?

I have tried the code given below to change the focus on keypress.

package focusdemo1;

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author vikassingh
 */
public class FocusDemo1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField username = new TextField();
        TextField password = new TextField();

        VBox mm = new VBox();
        mm.getChildren().addAll(username, password);

        StackPane root = new StackPane();
        root.getChildren().addAll(mm);

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

        root.requestFocus();
        // Key Listner
        root.setOnKeyPressed(new EventHandler<KeyEvent>() {

            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.DOWN) {
                    username.requestFocus();
                    event.consume();
                } else {
                    try {
                        keyAction(event.getCode().toString(), root, primaryStage);
                    } catch (Exception ex) {
                        Logger.getLogger(FocusDemo1.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

            }
        });
        username.setOnKeyPressed(new EventHandler<KeyEvent>() {
            public void handle(KeyEvent event) {
                if (event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB || event.getCode() == KeyCode.DOWN) {
                    password.requestFocus();
                    event.consume();
                }
            }
        });

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

    }

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

    // F1 & Esc Key Action
    public void keyAction(String keyCode, StackPane root, Stage rootStage) throws Exception {
        root.requestFocus();
        if (keyCode.equalsIgnoreCase("F1")) {
            root.getChildren().removeAll();
            //LoginSettings primaryPage = new LoginSettings(root, rootStage);
            root.requestFocus();
        }
        if (keyCode.equalsIgnoreCase("F2")) {
            root.getChildren().removeAll();
            //ForgotPassword forgotPasswordPage = new ForgotPassword(root, rootStage);
            root.requestFocus();
        }
        if (keyCode.equalsIgnoreCase("ESCAPE")) {
            if (!root.getId().toString().equalsIgnoreCase("LoginPage")) {
                root.getChildren().removeAll();
                // Login loginPage = new Login(root, rootStage);
                root.requestFocus();
            }
        }
    }
    // End F1 & Esc Key Action

}

But the problem I am facing now is that, whenever I press SHIFT key the focus is removed from the Textfield. For eg. I have tried to enter "Vikas" where Capital "V" (SHIFT + V).

Upvotes: 0

Views: 588

Answers (1)

Inge
Inge

Reputation: 447

the problem is the first call to root.requestFocus() in your keyAction function. Try to comment it out and give it a try.

Upvotes: 0

Related Questions