I have answered How to add letter spacing in Swing. However, this way does not seem to have impact for JPasswordField
(s):
When I passwordField.setEchoChar((char)0)
, it works:
Is there a way to apply the spacing when the dots (•) are shown?
public class PasswordFieldLetterSpacing {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JPasswordField field = new JPasswordField(15);
Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
attributes.put(TextAttribute.TRACKING, 0.5);
field.setFont(field.getFont().deriveFont(attributes));
JLabel showPassLabel = new JLabel("move mouse here");
showPassLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
showPassLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent ev) {
field.setEchoChar((char) 0);
}
@Override
public void mouseExited(MouseEvent ev) {
field.setEchoChar((char) UIManager.get("PasswordField.echoChar"));
}
});
JFrame frame = new JFrame("");
frame.setLayout(new FlowLayout());
frame.add(field);
frame.add(showPassLabel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setVisible(true);
});
}
}
I don't know if look and feel plays a role on this. It seems to happen in both Windows Look and Feel and in the Java's default one.