Example usage for javafx.scene.control PasswordField textProperty

List of usage examples for javafx.scene.control PasswordField textProperty

Introduction

In this page you can find the example usage for javafx.scene.control PasswordField textProperty.

Prototype

public final StringProperty textProperty() 

Source Link

Usage

From source file:Main.java

License:asdf

@Override
public void start(Stage primaryStage) {
    User user = new User();
    Group root = new Group();
    Scene scene = new Scene(root, 320, 100);
    primaryStage.setScene(scene);//from www .j  a  va  2  s. c  om

    Text userName = new Text();
    userName.textProperty().bind(user.userNameProperty());

    PasswordField passwordField = new PasswordField();
    passwordField.setPromptText("Password");
    user.passwordProperty().bind(passwordField.textProperty());

    // user hits the enter key
    passwordField.setOnAction(actionEvent -> {
        if (accessGranted.get()) {
            System.out.println("granted access:" + user.getUserName());
            System.out.println("password:" + user.getPassword());
            Platform.exit();
        } else {
            primaryStage.setTitle("no access");
        }
    });

    passwordField.textProperty().addListener((obs, ov, nv) -> {
        boolean granted = passwordField.getText().equals(MY_PASS);
        accessGranted.set(granted);
        if (granted) {
            primaryStage.setTitle("");
        }
    });
    VBox formLayout = new VBox(4);
    formLayout.getChildren().addAll(userName, passwordField);
    formLayout.setLayoutX(12);
    formLayout.setLayoutY(12);

    root.getChildren().addAll(formLayout);
    primaryStage.show();
}

From source file:User.java

private HBox drawRow2() {
    PasswordField passwordField = new PasswordField();
    passwordField.setFont(Font.font("SanSerif", 20));
    passwordField.setPromptText("Password");
    passwordField.setStyle(//from  w  ww .  j  a va  2  s. c  o  m
            "-fx-text-fill:black; " + "-fx-prompt-text-fill:gray; " + "-fx-highlight-text-fill:black; "
                    + "-fx-highlight-fill: gray; " + "-fx-background-color: rgba(255, 255, 255, .80); ");

    passwordField.prefWidthProperty().bind(primaryStage.widthProperty().subtract(55));
    user.passwordProperty().bind(passwordField.textProperty());

    // error icon
    SVGPath deniedIcon = new SVGPath();
    deniedIcon.setFill(Color.rgb(255, 0, 0, .9));
    deniedIcon.setStroke(Color.WHITE);//
    deniedIcon.setContent("M24.778,21.419 19.276,15.917 24.777,10.415 21.949,7.585 "
            + "16.447,13.08710.945,7.585 8.117,10.415 13.618,15.917 8.116,21.419 10"
            + ".946,24.248 16.447,18.746 21.948,24.248z");
    deniedIcon.setVisible(false);

    SVGPath grantedIcon = new SVGPath();
    grantedIcon.setFill(Color.rgb(0, 255, 0, .9));
    grantedIcon.setStroke(Color.WHITE);//
    grantedIcon.setContent(
            "M2.379,14.729 5.208,11.899 12.958,19.648 25.877," + "6.733 28.707,9.56112.958,25.308z");
    grantedIcon.setVisible(false);

    //
    StackPane accessIndicator = new StackPane();
    accessIndicator.getChildren().addAll(deniedIcon, grantedIcon);
    accessIndicator.setAlignment(Pos.CENTER_RIGHT);

    grantedIcon.visibleProperty().bind(GRANTED_ACCESS);

    // user hits the enter key
    passwordField.setOnAction(actionEvent -> {
        if (GRANTED_ACCESS.get()) {
            System.out.printf("User %s is granted access.\n", user.getUserName());
            System.out.printf("User %s entered the password: %s\n", user.getUserName(), user.getPassword());
            Platform.exit();
        } else {
            deniedIcon.setVisible(true);
        }

        ATTEMPTS.set(ATTEMPTS.add(1).get());
        System.out.println("Attempts: " + ATTEMPTS.get());
    });

    // listener when the user types into the password field
    passwordField.textProperty().addListener((obs, ov, nv) -> {
        boolean granted = passwordField.getText().equals(MY_PASS);
        GRANTED_ACCESS.set(granted);
        if (granted) {
            deniedIcon.setVisible(false);
        }
    });

    // listener on number of attempts
    ATTEMPTS.addListener((obs, ov, nv) -> {
        if (MAX_ATTEMPTS == nv.intValue()) {
            // failed attempts
            System.out.printf("User %s is denied access.\n", user.getUserName());
            Platform.exit();
        }
    });

    // second row
    HBox row2 = new HBox(3);
    row2.getChildren().addAll(passwordField, accessIndicator);

    return row2;
}

From source file:de.perdoctus.ebikeconnect.gui.dialogs.LoginDialog.java

@PostConstruct
public void init() {
    final ImageView graphic = new ImageView(
            new Image(getClass().getResource("/app-icon.png").toExternalForm()));
    graphic.setPreserveRatio(true);//from  ww w  .j  av  a2s  . co  m
    graphic.setFitHeight(64);
    setGraphic(graphic);
    setResizable(true);
    setWidth(400);
    setResizable(false);

    setTitle(rb.getString("dialogTitle"));
    setHeaderText(rb.getString("dialogMessage"));

    final ButtonType loginButtonType = new ButtonType(rb.getString("loginButton"),
            ButtonBar.ButtonData.OK_DONE);
    getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);

    // Create the username and password labels and fields.
    final GridPane grid = new GridPane();
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(20, 10, 10, 10));
    grid.setPrefWidth(getWidth());
    grid.getColumnConstraints().add(new ColumnConstraints(-1, -1, -1, Priority.NEVER, HPos.LEFT, true));
    grid.getColumnConstraints().add(new ColumnConstraints(-1, -1, -1, Priority.ALWAYS, HPos.LEFT, true));

    final String rbUsername = rb.getString(CFG_USERNAME);
    final TextField txtUsername = new TextField();
    txtUsername.setPromptText(rbUsername);
    txtUsername.setText(config.getString(CFG_USERNAME, ""));

    final Label lblUsername = new Label(rbUsername);
    lblUsername.setLabelFor(txtUsername);
    grid.add(lblUsername, 0, 0);
    grid.add(txtUsername, 1, 0);

    final String rbPassword = rb.getString(CFG_PASSWORD);
    final PasswordField txtPassword = new PasswordField();
    txtPassword.setPromptText(rbPassword);
    if (config.getBoolean(CFG_SAVE_PASSWORD, false)) {
        txtPassword.setText(config.getString(CFG_PASSWORD, ""));
    }

    final Label lblPassword = new Label(rbPassword);
    lblPassword.setLabelFor(txtPassword);
    grid.add(lblPassword, 0, 1);
    grid.add(txtPassword, 1, 1);

    final CheckBox cbSavePassword = new CheckBox(rb.getString("save-password"));
    cbSavePassword.setSelected(config.getBoolean(CFG_SAVE_PASSWORD, false));
    grid.add(cbSavePassword, 1, 2);

    getDialogPane().setContent(grid);

    // Enable/Disable login button depending on whether a username was entered.
    final Node loginButton = getDialogPane().lookupButton(loginButtonType);
    loginButton.disableProperty()
            .bind(txtUsername.textProperty().isEmpty().or(txtPassword.textProperty().isEmpty()));

    setResultConverter(buttonType -> {
        if (buttonType == loginButtonType) {
            config.setProperty(CFG_USERNAME, txtUsername.getText());
            config.setProperty(CFG_SAVE_PASSWORD, cbSavePassword.isSelected());
            if (cbSavePassword.isSelected()) {
                config.setProperty(CFG_PASSWORD, txtPassword.getText());
                config.setProperty(CFG_PASSWORD, txtPassword.getText());
            } else {
                config.clearProperty(CFG_PASSWORD);
            }
            return new Credentials(txtUsername.getText(), txtPassword.getText());
        } else {
            return null;
        }
    });

    if (txtUsername.getText().isEmpty()) {
        txtUsername.requestFocus();
    } else {
        txtPassword.requestFocus();
        txtPassword.selectAll();
    }
}

From source file:com.danilafe.sbaccountmanager.StarboundServerAccountManager.java

private void createAccount(ListView<String> to_update) {
    Stage createAccountStage = new Stage();
    createAccountStage.initModality(Modality.APPLICATION_MODAL);

    //Set the stage info
    createAccountStage.setTitle("Add Server Account");

    //Create a layout
    GridPane gp = new GridPane();
    gp.setAlignment(Pos.CENTER);/* ww w.j  a v a 2 s .co m*/
    gp.setVgap(10);
    gp.setHgap(10);
    gp.setPadding(new Insets(25, 25, 25, 25));

    //Ads the important things
    Text welcome = new Text("Create Server Account");
    welcome.setFont(Font.font("Century Gothic", FontWeight.NORMAL, 20));
    gp.add(welcome, 0, 0, 2, 1);

    Label username = new Label("Username");
    Label password = new Label("Password");
    Label r_password = new Label("Repeat Password");

    TextField usernamefield = new TextField();
    PasswordField passwordfield = new PasswordField();
    PasswordField r_passwordfield = new PasswordField();

    gp.add(username, 0, 1);
    gp.add(password, 0, 2);
    gp.add(r_password, 0, 3);

    gp.add(usernamefield, 1, 1);
    gp.add(passwordfield, 1, 2);
    gp.add(r_passwordfield, 1, 3);

    Text error = new Text("");
    HBox error_box = new HBox(10);
    error_box.setAlignment(Pos.CENTER);
    error_box.getChildren().add(error);
    gp.add(error_box, 0, 4, 2, 1);

    Button finish = new Button("Finish");
    finish.setDisable(true);
    HBox center_button = new HBox(10);
    center_button.setAlignment(Pos.CENTER);
    center_button.getChildren().add(finish);
    gp.add(center_button, 0, 5, 2, 1);

    ChangeListener name = new ChangeListener<String>() {

        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
            finish.setDisable(true);
            if (usernamefield.getText().equals("")) {
                error.setFill(Color.RED);
                error.setText("Username can not be blank!");
            }
            if (!passwordfield.getText().equals(r_passwordfield.getText())) {
                error.setFill(Color.RED);
                error.setText("Passwords do not match!");
            }
            if (passwordfield.getText().equals("") && r_passwordfield.getText().equals("")) {
                error.setFill(Color.RED);
                error.setText("Passwords can not be blank!");
            }
            if (passwordfield.getText().equals(r_passwordfield.getText()) && !usernamefield.getText().equals("")
                    && !passwordfield.getText().equals("")) {
                error.setFill(Color.GREEN);
                error.setText("No issues.");
                finish.setDisable(false);
            }
        }

    };

    usernamefield.textProperty().addListener(name);
    passwordfield.textProperty().addListener(name);
    r_passwordfield.textProperty().addListener(name);

    finish.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent e) {

            users.remove(usernamefield.getText());
            users.add(usernamefield.getText());
            userpasswords.put(usernamefield.getText(), passwordfield.getText());
            to_update.setItems(FXCollections.observableArrayList(users));

            createAccountStage.close();
        }

    });

    //Creates the scene
    Scene scene = new Scene(gp, 300, 275);
    scene.getStylesheets().add(this.getClass().getResource("login_css.css").toExternalForm());

    createAccountStage.setScene(scene);
    createAccountStage.show();

}