Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove default focus from TextField JavaFX

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?

like image 431
Yashar Avatar asked Mar 14 '15 16:03

Yashar


3 Answers

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.

like image 128
dermoritz Avatar answered Oct 17 '22 23:10

dermoritz


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.

like image 40
Helmwag Avatar answered Oct 17 '22 23:10

Helmwag


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 :

enter image description here

like image 4
ItachiUchiha Avatar answered Oct 18 '22 01:10

ItachiUchiha