6509273: Password in JPasswordField gets Printed in clear text
authorrupashka
Mon, 18 Jul 2011 17:40:12 +0400
changeset 10099 1887eb63f84c
parent 10098 0674614189ba
child 10100 c525c5fbb86c
6509273: Password in JPasswordField gets Printed in clear text Reviewed-by: alexp
jdk/src/share/classes/sun/swing/text/TextComponentPrintable.java
--- a/jdk/src/share/classes/sun/swing/text/TextComponentPrintable.java	Fri Jul 15 19:25:06 2011 +0400
+++ b/jdk/src/share/classes/sun/swing/text/TextComponentPrintable.java	Mon Jul 18 17:40:12 2011 +0400
@@ -47,15 +47,7 @@
 import java.util.concurrent.FutureTask;
 import java.util.concurrent.atomic.AtomicReference;
 
-import javax.swing.BorderFactory;
-import javax.swing.CellRendererPane;
-import javax.swing.JTextField;
-import javax.swing.JTextArea;
-import javax.swing.JEditorPane;
-import javax.swing.JViewport;
-import javax.swing.JScrollPane;
-import javax.swing.JTextPane;
-import javax.swing.SwingUtilities;
+import javax.swing.*;
 import javax.swing.border.Border;
 import javax.swing.border.TitledBorder;
 import javax.swing.text.BadLocationException;
@@ -336,7 +328,22 @@
         assert SwingUtilities.isEventDispatchThread();
 
         JTextComponent ret = null;
-        if (textComponent instanceof JTextField) {
+        if (textComponent instanceof JPasswordField) {
+            ret =
+                new JPasswordField() {
+                    {
+                        setEchoChar(((JPasswordField) textComponent).getEchoChar());
+                        setHorizontalAlignment(
+                            ((JTextField) textComponent).getHorizontalAlignment());
+                    }
+                    @Override
+                    public FontMetrics getFontMetrics(Font font) {
+                        return (frc.get() == null)
+                            ? super.getFontMetrics(font)
+                            : FontDesignMetrics.getMetrics(font, frc.get());
+                    }
+                };
+        } else if (textComponent instanceof JTextField) {
             ret =
                 new JTextField() {
                     {