1 7 package javax.swing; 8 9 import javax.swing.text.*; 10 import javax.swing.plaf.*; 11 import javax.accessibility.*; 12 13 import java.io.ObjectOutputStream ; 14 import java.io.ObjectInputStream ; 15 import java.io.IOException ; 16 17 47 public class JPasswordField extends JTextField { 48 49 54 public JPasswordField() { 55 this(null,null,0); 56 } 57 58 65 public JPasswordField(String text) { 66 this(null, text, 0); 67 } 68 69 76 public JPasswordField(int columns) { 77 this(null, null, columns); 78 } 79 80 88 public JPasswordField(String text, int columns) { 89 this(null, text, columns); 90 } 91 92 106 public JPasswordField(Document doc, String txt, int columns) { 107 super(doc, txt, columns); 108 echoChar = '*'; 109 enableInputMethods(false); 113 } 114 115 122 public String getUIClassID() { 123 return uiClassID; 124 } 125 126 127 134 public char getEchoChar() { 135 return echoChar; 136 } 137 138 153 public void setEchoChar(char c) { 154 echoChar = c; 155 repaint(); 156 revalidate(); 157 } 158 159 168 public boolean echoCharIsSet() { 169 return echoChar != 0; 170 } 171 172 174 182 public void cut() { 183 if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) { 184 UIManager.getLookAndFeel().provideErrorFeedback(this); 185 } else { 186 super.cut(); 187 } 188 } 189 190 198 public void copy() { 199 if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) { 200 UIManager.getLookAndFeel().provideErrorFeedback(this); 201 } else { 202 super.copy(); 203 } 204 } 205 206 217 @Deprecated 218 public String getText() { 219 return super.getText(); 220 } 221 222 235 @Deprecated 236 public String getText(int offs, int len) throws BadLocationException { 237 return super.getText(offs, len); 238 } 239 240 249 public char[] getPassword() { 250 Document doc = getDocument(); 251 Segment txt = new Segment(); 252 try { 253 doc.getText(0, doc.getLength(), txt); } catch (BadLocationException e) { 255 return null; 256 } 257 char[] retValue = new char[txt.count]; 258 System.arraycopy(txt.array, txt.offset, retValue, 0, txt.count); 259 return retValue; 260 } 261 262 266 private void writeObject(ObjectOutputStream s) throws IOException { 267 s.defaultWriteObject(); 268 if (getUIClassID().equals(uiClassID)) { 269 byte count = JComponent.getWriteObjCounter(this); 270 JComponent.setWriteObjCounter(this, --count); 271 if (count == 0 && ui != null) { 272 ui.installUI(this); 273 } 274 } 275 } 276 277 279 283 private static final String uiClassID = "PasswordFieldUI"; 284 285 private char echoChar; 286 287 288 297 protected String paramString() { 298 return super.paramString() + 299 ",echoChar=" + echoChar; 300 } 301 302 306 307 319 public AccessibleContext getAccessibleContext() { 320 if (accessibleContext == null) { 321 accessibleContext = new AccessibleJPasswordField(); 322 } 323 return accessibleContext; 324 } 325 326 341 protected class AccessibleJPasswordField extends AccessibleJTextField { 342 343 350 public AccessibleRole getAccessibleRole() { 351 return AccessibleRole.PASSWORD_TEXT; 352 } 353 } 354 } 355 | Popular Tags |