0

I just want to show the inputted value from PasswordField when ToggleButton is set to TRUE, the Bullets will convert into Text Character, pretty simple. I found this but unfortunately I'm stock because com.sun.javafx.scene.control.behavior.PasswordFieldBehavior is cannot resolve.

Normally, how are we or how should a PasswordField will be able to convert from Bullets to Text Character? is there any function for it?

Polar
  • 3,327
  • 4
  • 42
  • 77
  • [MaterialFX](https://github.com/palexdev/MaterialFX/wiki/Text-Fields#mfxpasswordfields) has this kind of functionality in its `MFXPasswordField`. – jewelsea Jul 06 '23 at 23:30

2 Answers2

4

You can stack another TextField and bind their values.

final StackPane sp = new StackPane();
final PasswordField pwf = new PasswordField();
final TextField tf = new TextField();
final ToggleButton toggle = new ToggleButton();

sp.getChildren().addAll(pwf, tf);

pwf.textProperty().bindBidirectional(tf.textProperty());
pwf.visibleProperty().bind(toggle.selectedProperty().not());
tf.visibleProperty().bind(toggle.selectedProperty());

Depending on how your sizing strategy for the two input controls, you may need to call #setManaged(false) for the text field.

Jai
  • 8,165
  • 2
  • 21
  • 52
  • I just realized this is totally the same thing as the link you gave. It's the second solution provided in the answer. – Jai Jun 19 '18 at 07:39
  • I thought there's a function for it. . . too bad for JavaFX. . . Anyway, Thank you so much! – Polar Jun 19 '18 at 08:07
  • @Jai not exactly the same as the second solution in the link... you added the StackPane, which is a lot more clear – negste Nov 15 '18 at 13:32
0

You have to write your own skin by subclassing TextFieldSkin and overriding maskText(String txt) there to return the original text instead of bullets.

Here is an executable example:

package application;

import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.skin.TextFieldSkin;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        HBox root = new HBox();

        PasswordField passwordField = new PasswordField();

        BooleanProperty showPassword = new SimpleBooleanProperty() {
            @Override
            protected void invalidated() {
                // force maskText to be called
                String txt = passwordField.getText();
                passwordField.setText(null);
                passwordField.setText(txt);
            }
        };

        passwordField.setSkin(new TextFieldSkin(passwordField) {
            @Override
            protected String maskText(String txt) {
                if (showPassword.get()) {
                    return txt;
                }
                return super.maskText(txt);
            }
        });

        ToggleButton bulletToggle = new ToggleButton("Show Password");
        showPassword.bind(bulletToggle.selectedProperty());

        root.getChildren().addAll(passwordField, bulletToggle);

        Scene scene = new Scene(root);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
isnot2bad
  • 24,105
  • 2
  • 29
  • 50