jobr97 jobr97 - 1 year ago 166
Java Question

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

. 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?

Answer Source

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 {

    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 ;
            if ("secret".equals(typedText.toString())) {
                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());


    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);

    public static void main(String[] args) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download