Problem with PasswordField.setText()

Hallo@All

I start to discover echo3 and so wrote a small application with a LoginView.


public class LoginView extends WindowPane {

    private Label userNameLable;

    private TextField userNameInput;

    private Label passwordNameLable;

    private PasswordField userPasswordField;

    private Button okButton;

    private Button cancelButton;


    public LoginView() {
        this.setClosable(false);
        this.setMovable(false);
        this.setResizable(false);
        this.setTitle("Login");

        Grid grid = new Grid();
        grid.setOrientation(Grid.ORIENTATION_HORIZONTAL);
        grid.setSize(2);
        add(grid);

        grid.add(getUserNameLable());
        grid.add(getUserNameInput());

        grid.add(getPasswordLabel());
        grid.add(getUserPasswordField());

        grid.add(getOkButton());
        grid.add(getCancelButton());
    }


    Label getUserNameLable() {
        return this.userNameLable != null ? this.userNameLable : new Label("Name:");
    }


    TextField getUserNameInput() {
        if (this.userNameInput == null) {
            this.userNameInput = new TextField();
        }
        return this.userNameInput;
    }


    Label getPasswordLabel() {
        return this.passwordNameLable != null ? this.passwordNameLable : new Label("Password:");
    }


    PasswordField getUserPasswordField() {
        return this.userPasswordField != null ? this.userPasswordField : new PasswordField();
    }


    Button getOkButton() {
        return this.okButton != null ? this.okButton : new Button("OK");
    }


    Button getCancelButton() {
        if (this.cancelButton == null) {
            this.cancelButton = new Button("CANCEL");
            this.cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    getUserNameInput().setText("");
                    getUserPasswordField().setText("");
                }
            });
        }
        return this.cancelButton;
    }
}

For my understanding, when I press the "Cancel" button, the input for the username and password should be reset.

For username it works, the PasswordField is unaffected.

Where is the Bug / some hints anyone?

Thanks@all.

mjablonski's picture

It's your bug...:)

Please compare your lazy initialisation for getUserNameInput and for getUserPasswordField. userPasswordField is never stored to the instance variable, so always a new PasswordField is returned.

Cheers, Maik

TextField getUserNameInput() {
        if (this.userNameInput == null) {
            this.userNameInput = new TextField();
        }
        return this.userNameInput;
    }
    ...

   PasswordField getUserPasswordField() {
        return this.userPasswordField != null ? this.userPasswordField : new PasswordField();
    }

thx, it works ...