如何取消屏蔽 JavaFX PasswordField 或正确屏蔽 TextField? [英] How to unmask a JavaFX PasswordField or properly mask a TextField?

查看:25
本文介绍了如何取消屏蔽 JavaFX PasswordField 或正确屏蔽 TextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的用户界面中,我有一个 :

public class PasswordFieldSkin extends TextFieldSkin {public static final char BULLET = 'u2022';公共密码字段皮肤(密码字段密码字段){超级(密码字段,新密码字段行为(密码字段));}@Override protected String maskText(String txt) {TextField textField = getSkinnable();int n = textField.getLength();StringBuilder passwordBuilder = new StringBuilder(n);for (int i=0; i<n; i++) {passwordBuilder.append(BULLET);}返回 passwordBuilder.toString();}}

最后,这是使用绑定显示密码字符的演示应用程序:

@Override公共无效开始(阶段primaryStage){//将密码显示为未屏蔽的文本字段final TextField textField = new TextField();//设置初始状态textField.setManaged(false);textField.setVisible(false);//实际密码字段final PasswordField passwordField = new PasswordField();CheckBox checkBox = new CheckBox("显示/隐藏密码");//绑定属性.切换 textField 和 passwordField//当复选框的状态改变时,可见性和可管理性属性相互关联.//因为我们只想显示一个组件(textField 或 passwordField)//一次在场景中.textField.managedProperty().bind(checkBox.selectedProperty());textField.visibleProperty().bind(checkBox.selectedProperty());passwordField.managedProperty().bind(checkBox.selectedProperty().not());passwordField.visibleProperty().bind(checkBox.selectedProperty().not());//双向绑定 textField 和 passwordField 文本值.textField.textProperty().bindBidirectional(passwordField.textProperty());VBox 根 = 新 VBox(10);root.getChildren().addAll(passwordField, textField, checkBox);场景场景 = 新场景(root, 300, 250);primaryStage.setTitle("演示");primaryStage.setScene(场景);primaryStage.show();}

In a UI of mine, I have a PasswordField like so (urm the one at the bottom!):

I want a user to be able to check the checkbox you see in the picture and have all "secret" password characters displayed. Not much different from the option we get from many modern password-asking UI:s floating around. However, I cannot find anything in the JavaFX API that let me do that?

If my worries hold true, then I would like to use a TextField that display the last key pressed for only half a second or until next key is pressed, and then he shall mask all previous user input. This would produce a cool animation effect that one can see sometimes in modern UI:s. However, is there a way for me to get hold of the OS dependent (I think it is OS dependent??) password echo character I should use?

If it is not possible to get that OS dependent character, then I'd be glad to use the character you see on the picture (JavaFX on a Windows 8 machine). What is the UTF-8 code point for this stranger?

解决方案

> However, I cannot find anything in the JavaFX API that let me do that?

The PasswordField component does not display masked text by default. However you can use PasswordField with TextField and toggle masked/unmasked text using these components respectively. Where the unmasked text is shown by TextField, as in example demo below.

> I would like to use a TextField that display the last key pressed for only half a second or until next key is pressed, and then he shall mask all previous user input.

Since PasswordField, itself is a extended version of TextField. You can always build your own custom password textbox with properties you mentioned.

> is there a way for me to get hold of the OS dependent (I think it is OS dependent??) password echo character I should use?

Frankly did not grab what you are saying here. You can track text changes by adding change listener to PasswordField.textPrperty() and do animations, timers etc. You can override the default bullet mask by extending PasswordFieldSkin and using it through CSS -fx-skin. See the definition of bullet in its source here:

public class PasswordFieldSkin extends TextFieldSkin {
    public static final char BULLET = 'u2022';

    public PasswordFieldSkin(PasswordField passwordField) {
        super(passwordField, new PasswordFieldBehavior(passwordField));
    }

    @Override protected String maskText(String txt) {
        TextField textField = getSkinnable();

        int n = textField.getLength();
        StringBuilder passwordBuilder = new StringBuilder(n);
        for (int i=0; i<n; i++) {
            passwordBuilder.append(BULLET);
        }

        return passwordBuilder.toString();
    }
}

Finally, Here is kick off demo app of showing password characters using bindings:

@Override
public void start(Stage primaryStage) {

    // text field to show password as unmasked
    final TextField textField = new TextField();
    // Set initial state
    textField.setManaged(false);
    textField.setVisible(false);

    // Actual password field
    final PasswordField passwordField = new PasswordField();

    CheckBox checkBox = new CheckBox("Show/Hide password");

    // Bind properties. Toggle textField and passwordField
    // visibility and managability properties mutually when checkbox's state is changed.
    // Because we want to display only one component (textField or passwordField)
    // on the scene at a time.
    textField.managedProperty().bind(checkBox.selectedProperty());
    textField.visibleProperty().bind(checkBox.selectedProperty());

    passwordField.managedProperty().bind(checkBox.selectedProperty().not());
    passwordField.visibleProperty().bind(checkBox.selectedProperty().not());

    // Bind the textField and passwordField text values bidirectionally.
    textField.textProperty().bindBidirectional(passwordField.textProperty());

    VBox root = new VBox(10);
    root.getChildren().addAll(passwordField, textField, checkBox);
    Scene scene = new Scene(root, 300, 250);
    primaryStage.setTitle("Demo");
    primaryStage.setScene(scene);
    primaryStage.show();
}

这篇关于如何取消屏蔽 JavaFX PasswordField 或正确屏蔽 TextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆