remove default focus from TextField JavaFX

老子叫甜甜 提交于 2019-12-06 03:02:31

问题


I have designed some TextField in a form by SceneBuilder, when I run the code, one of the TextFields has been clicked by default, I want when I run the code, none of the TextFields get selected by default and user select a TextFiled.

UPDATE: As you see in this image I want to make the first field like other two field when code runs(no curser in field)

How can I do this?


回答1:


As there is no public method to achieve this, there is no direct way. Though, you can use a trick to do it. You can have a BooleanProperty just to check when the control is focused for the first time. Listen to focusProperty() of the control and when it is focused for the first time, delegate the focus to its container. For the rest of the focus, it will work as it should.

Example:

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        final BooleanProperty firstTime = new SimpleBooleanProperty(true); // Variable to store the focus on stage load
        VBox vBox = new VBox(10);
        vBox.setPadding(new Insets(20));
        TextField t1 = new TextField();
        TextField t2 = new TextField();
        TextField t3 = new TextField();
        t1.setPromptText("FirstName");
        t2.setPromptText("LastName");
        t3.setPromptText("Email");
        vBox.getChildren().addAll(new HBox(t1, t2), t3);
        primaryStage.setScene(new Scene(vBox, 300, 300));
        primaryStage.show();
        t1.focusedProperty().addListener((observable,  oldValue,  newValue) -> {
            if(newValue && firstTime.get()){
                vBox.requestFocus(); // Delegate the focus to container
                firstTime.setValue(false); // Variable value changed for future references
            }
        });
    }

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

On initial screen load :




回答2:


In my case the accepted answer is not working. But this worked:

i requested focus for parent wrapped in runLater.

@FXML
public void initialize() {

    //unfocus pathField
    Platform.runLater( () -> root.requestFocus() );
}

Direct call of requestFocuss does not work.




回答3:


Had the same problem with a non - editable TextField, which got selected and highlighted every time.

Solved it by setting myTextField.setFocusTraversable(false);

Note that in this case the focus goes to the next UI element an you must set every single element you don't want focused. You lose also the ability to select the element via the Tab key.

The ApiDoc states that setFocusTraversable() is false by default, but that seems not to work, unless explicitly called.




回答4:


You can simply set the focus traversable to false in the initialize method. Here is an example:

@Override
public void initialize(URL location, ResourceBundle resources) {
    yourTextField.setFocusTraversable(false);
}


来源:https://stackoverflow.com/questions/29051225/remove-default-focus-from-textfield-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!