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.