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

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

问题描述

在我的UI中,我有一个 PasswordField 像这样( urm底部的那个!):

我希望用户能够选中您在图片中看到的复选框,并显示所有秘密"密码字符.与我们从许多现代的密码询问UI中获得的选项没有太大不同: 但是,我在JavaFX API中找不到让我做到这一点的任何东西吗?

如果我的担心仍然成立,那么我想使用TextField来显示最后按下的键仅半秒钟或直到按下下一个键,然后他将掩盖所有先前的用户输入.这将产生一种很酷的动画效果,人们有时可以在现代UI中看到它:s.但是,有没有办法让我掌握与操作系统有关的信息(我认为这与操作系统有关?)我应该使用的密码回显字符?

如果无法获得该依赖于操作系统的字符,那么我很高兴使用您在图片上看到的字符(Windows 8计算机上的JavaFX).这个陌生人的UTF-8代码点是什么?

解决方案

> 但是,我在JavaFX API中找不到让我做到这一点的东西吗?

默认情况下,PasswordField组件不显示被屏蔽的文本.但是,您可以将PasswordFieldTextField结合使用,并分别使用这些组件来切换带遮罩的文本.未遮罩的文本用TextField显示的位置,如下面的示例演示中所示.

> 我想使用一个TextField,它只显示最后一次按下的按键半秒钟,直到显示下一个按键为止,然后他将掩盖所有以前的用户输入./em>

PasswordField起,其本身是TextField的扩展版本.您始终可以使用提到的属性来构建自己的自定义密码文本框.

> 有没有办法让我掌握应该使用的依赖于操作系统(我认为是依赖于操作系统??)的密码回显字符?

坦率地说,您没有抓住您在这里说的话.您可以通过将更改侦听器添加到PasswordField.textPrperty()并执行动画,计时器等来跟踪文本更改.您可以通过扩展PasswordFieldSkin并通过CSS -fx-skin使用它来覆盖默认的项目符号蒙版.请参见

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天全站免登陆