'Set PlaceHolder on JPasswordField

I am developing a system with Java (using NetBeans) and, to make it more "professional", I've added some cool functions, such as Placeholders (Yes, I know, it's from HTML).

For ALL the JTextFields I have, I've used the following code to generate their placeholders (The name of the JTextField in this example is "tfUser") :

private void tfUserFocusGained(java.awt.event.FocusEvent evt) {
    if (tfUser.getText().equals("Your User Name...")) {
        tfUser.setText("");
        tfUser.setForeground(Color.BLACK);
    }
}

private void tfUserFocusLost(java.awt.event.FocusEvent evt) {                                    
    if (tfUser.getText().equals("")) {
        tfUser.setText("Your User Name...");
        tfUser.setForeground(Color.GRAY);
    }
}

It's a "focus match": The Text Field has initially the text "Your User Name...", with a "GRAY" foreground. Every time this text field gains the focus, it verifies its text: if the text.equals("Your User Name..."), its text is set to "" (An empty String) and the Foreground is set to BLACK (default). On the other hand, if the text.equals("Anything else..."), it means that the user has probably inserted the user name, so, do not do anything with this code.

Every time the text field loses the focus, it verifies its text: if the text.equals("") (An empty String again), its text is set back to "Your User Name..." and the Foreground is set to GRAY. And again, if the text.equals("Anything else..."), it means that the user has probably inserted the user name, so, do not do anything with this code.

This code is working perfectly with the JTextFields But, when I do the same with JPasswordFields, I get the following result:

****************  

(It should be "Your Password...")

Can anyone help me to add a placeholder to this JPasswordField? Thanks in advance.



Solution 1:[1]

What i did for my login code was add a checkbox that "shows password" and in the jframe i added this statement:

//Setting checkbox selected to true so the word "password" is seen when program runs
 passCheckBox.setSelected(true);
    if(passCheckBox.isSelected())
    {
        PasswordField.setEchoChar((char)0);
    }`

Code for password checkbox:

if(passCheckBox.isSelected())
        {
            PasswordField.setEchoChar((char)0);
        }else{
            
            PasswordField.setEchoChar('*');
        }

Code for Password Field Focus Gained:

private void PasswordFieldFocusGained(java.awt.event.FocusEvent evt) {                                          
    passCheckBox.setSelected(false);
    PasswordField.setEchoChar('*');
    String password = String.valueOf(PasswordField.getPassword());
    
    if(password.toLowerCase().equals("password"))
    {
        PasswordField.setText("");
        PasswordField.setForeground(Color.black);
    }
    
}     

 

Code for Password Field focus lost:

private void PasswordFieldFocusLost(java.awt.event.FocusEvent evt) {                                        
     
    String password = String.valueOf(PasswordField.getPassword());
    
    
    if(password.toLowerCase().equals("password") || password.toLowerCase().equals("") )
    {
        PasswordField.setText("Password");
        PasswordField.setEchoChar((char)0);
        PasswordField.setForeground(new Color(153, 153, 153));
    }
}                

I kinda did half of this on my own and took some bits from videos i hope this helps :>

Solution 2:[2]

Here is my method for getting the placeholders

private void getPlaceholders(JTextField text) {

        String temp = text.getText();

        text.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                text.setText("");
                text.setForeground(Color.BLACK);

            }

            @Override
            public void focusLost(FocusEvent e) {
                if (text.getText().isEmpty()) {
                    text.setForeground(Color.GRAY);
                    text.setText(temp);
                }

            }
        });
    }

Here is how i implemented it.

//      Password Field
        passwordField = new JPasswordField();
        passwordField.setBounds(48, 340, 288, 32);
        roundedPanel.add(passwordField);
        passwordField.setText("Password");
        passwordField.setEchoChar((char) 0);
        getPlaceholders(passwordField);

what allows you to see placeholders in passwordFields is

passwordField.setEchoChar((char) 0);

you can take it out to use in regular text fields

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 wavydeer