jobr97
jobr97

Reputation: 179

Invisible Password

In one of my recent projects I want to implement a hidden page. I want to be able to reach it by just typing the password without anything showing on the screen. I tried to just set a PasswordField as visible(false). However that didn't work. Also I would like the hidden page to pop up without having to press enter after typing the password. Is there a way for a simple javafx application to behave like that?

Upvotes: 0

Views: 168

Answers (3)

James_D
James_D

Reputation: 209358

You can add an event filter to the scene that keeps track of what has been typed.

Here is a simple example (type "secret" with the main window focussed to show the popup window; press enter if you mistype to clear the hidden text):

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class OpenSecretWindow extends Application {

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("Type the secret password\nto open the secret window");
        label.setTooltip(new Tooltip("The secret password is \"secret\""));
        StackPane root = new StackPane(label);
        Scene scene = new Scene(root, 400, 400);

        StringBuilder typedText = new StringBuilder();
        scene.addEventFilter(KeyEvent.KEY_TYPED, e -> {
            switch(e.getCharacter()) {
            case "\n":
            case "\r":
                typedText.delete(0, typedText.length());
                break ;
            default:
                typedText.append(e.getCharacter());
            }
            if ("secret".equals(typedText.toString())) {
                openSecretWindow(primaryStage);
                typedText.delete(0, typedText.length());
            }
        });

        // handle backspace and delete:
        scene.addEventFilter(KeyEvent.KEY_RELEASED, e -> {
            if (e.getCode() == KeyCode.BACK_SPACE || e.getCode() == KeyCode.DELETE) {
                if (typedText.length() > 0) {
                    typedText.delete(typedText.length()-1, typedText.length());
                }
            }
        });

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

    private void openSecretWindow(Stage owner) {
        Stage stage = new Stage();
        StackPane root = new StackPane(new Label("You have found\nthe secret window!"));
        Scene scene = new Scene(root, 300, 180);
        stage.setScene(scene);
        stage.initOwner(owner);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 1

user7106805
user7106805

Reputation:

You could use a KeyListener. Though you need to press the screen once for the keypresses to register. And make sure to add the keylistener to the JFrame, I always forget that. This will look for keys, but requires a window to be shown, this can be empty though.

If you don't want a window at all, you can use the external library jnativehook it looks for keypresses globally.

Upvotes: 2

MarcElrick
MarcElrick

Reputation: 93

I believe you could set the foreground of the JPasswordField to be the same as the background colour but don't quote me on that. Something like:

JPasswordField.SetForeground(Color.RED);

Upvotes: 1

Related Questions