Original address http://download.oracle.com/javafx/2.0/ui_controls/password-field.htm#CHDIAAAJ
PasswordFieldClass implements a specific text box: the characters you enter into it are hidden, instead of displaying a special echo string. Figure
9-1 is a password box with prompts.
Figure 9-1 Password field with a prompt message
Description of "Figure 9-1 Password field with a prompt message"
Create Password Field
An entry-level task is to create a password box, see Example 9-1.
Example 9-1 Creating a password field
PasswordField passwordField = new PasswordField();passwordField.setPromptText("Your password");
Because it is a user interface, it is best to configure a prompt or an indication label for the password box. AndTextFieldSame class,The passwordfield class also provides setTextMethod To set the string to be displayed in the control after the application is started. However,setTextThe string specified by the method is overwritten by the echo character in the password box. By default, the echo character in the password box is an asterisk. Figure
9-2 is a password box with predefined text.
Figure 9-2 password field with the set text
Description of "Figure 9-2 password field with the set text"
The value in the password box can also use gettext.Method. You can process this value in your application and set the authentication logic.
Process the password value
Take a moment to read the code in Example 9-2. It implements a password box.
Example 9-2 Implementing the authentication Logic
final Label message = new Label("");VBox vb = new VBox();vb.setPadding(new Insets(10, 0, 0, 10));vb.setSpacing(10);HBox hb = new HBox();hb.setSpacing(10);hb.setAlignment(Pos.CENTER_LEFT);Label label = new Label("Password");final PasswordField pb = new PasswordField();pb.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { if (!pb.getText().equals("T2f$Ay!")) { message.setText("Your password is incorrect!"); message.setTextFill(Color.rgb(210, 39, 30)); } else { message.setText("Your password has been confirmed"); message.setTextFill(Color.rgb(21, 117, 84)); } pb.setText(" "); }});hb.getChildren().addAll(label, pb);vb.getChildren().addAll(hb, message);
UsesetOnActionThe authentication logic is defined in the password box. This method is called after the password is submitted, it createsEventHandlerObject. If the input value is different from the required value, the corresponding information is displayed in red. See figure
9-3.
Figure 9-3 password is incorrect
Description of "Figure 9-3 password is incorrect"
If the input value meets the requirements, the confirmation information is displayed, as shown in Figure 9-4.
Figure 9-4 password is correct
Description of "Figure 9-4 password is correct"
For security reasons, the best practice is to enter text and clear the Password box. In Example 9-2, the password is left blank after authentication.