简体   繁体   中英

TextField - getText() wont work

I have this code sample that implement input mask to TextField :

package com.example;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        TextField field = new TextField() {
            @Override public void replaceText(int start, int end, String text) {
                super.replaceText(start, end, "@");
            }
        };

        Button button = new Button("Show Text");
        button.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                System.out.println("TEXT: " + field.getText());
            }
        });

        VBox root = new VBox(20);
        root.setAlignment(Pos.CENTER);
        root.getChildren().addAll(field, button);

        Scene scene = new Scene(root, 500, 500);
        stage.setScene(scene);
        stage.show(); 
    }  

}

Whenever I type the word "LIFE" in the TextField and press the button, the output always returns TEXT: @@@@ , I want the output to return TEXT: LIFE . It seems like getText is not working. How to fix this?

Your approach doesn't work, because the replaceText method calls setText ; so the implementation in your subclass causes the text field's textProperty to contain only '@' characters. Hence when you call getText() , you get a string with all '@' characters.

If you want to use a subclass of TextField like that, you would have to keep track of the "real" text elsewhere, which would get quite difficult. (Also, I think your implementation doesn't behave properly with copy and paste, and fixing that would be a bit tricky too.)

Probably the way to do this is to use a PasswordField , and replace it with a TextField when you want to show the text.

Here's a simple example:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class RevealPasswordExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        PasswordField passwordField = new PasswordField();
        TextField textField = new TextField();
        passwordField.textProperty().bindBidirectional(textField.textProperty());

        StackPane textContainer = new StackPane(passwordField);

        CheckBox showText = new CheckBox("Show text");
        showText.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
            if (isNowSelected) {
                textContainer.getChildren().setAll(textField);
            } else {
                textContainer.getChildren().setAll(passwordField);
            }
        });

        VBox root = new VBox(5, textContainer, showText);

        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(24));

        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

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

Actually getText() is working properly, that's why you got @@@@ at the output. The thing is you have used replaceText() method, which will replace all the characters of your text field to '@' from the start to end. I donno why u have used that. If you don't wanna show the string typed in the field try it as a password field like,

PasswordField field = new PasswordField();
field.getText();

by using this also you can get the field text but it won't be visible to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM