As you can read from the headline I have some troubles organizing/solving a little issue. Wrote a class for passwordfield. I basically want to insert a placeholder to the passwordfield, which sets the hint(if you will) or default text to zero when user clicks on passwordfield to kinda show that the field is actually for password.
Want to built a login frame for a small project. Any answer is appreciated.
Issue contains don't know how to cut the default text(password text) to zero when user clicks on it
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class externPlaceholderPasswordField extends JPasswordField
{
private String pw;
public externPlaceholderPasswordField(String pw)
{
this.pw = pw;
}
public char[] getPassword()
{
char[] password = super.getPassword();
if(password.trim().length() == 0 && pw != null)
{
password = pw;
}
return password;
}
@Override
public void paintComponent(Graphics g)
{
setHorizontalAlignment(externPlaceholderTextField.CENTER);
super.paintComponent(g);
if (super.getPassword().length() > 0 || pw == null)
{
return;
}
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.lightGray);
g2.drawString(pw, getInsets().left, g.getFontMetrics().getMaxAscent() + getInsets().top);
}
}