8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package javax.swing;
26
27 import javax.swing.text.*;
28 import javax.swing.plaf.*;
29 import javax.accessibility.*;
30
31 import java.io.ObjectOutputStream;
32 import java.io.ObjectInputStream;
33 import java.io.IOException;
34 import java.io.*;
35 import java.util.Arrays;
36
37 /**
38 * <code>JPasswordField</code> is a lightweight component that allows
39 * the editing of a single line of text where the view indicates
40 * something was typed, but does not show the original characters.
41 * You can find further information and examples in
42 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html">How to Use Text Fields</a>,
43 * a section in <em>The Java Tutorial.</em>
44 * <p>
45 * <code>JPasswordField</code> is intended
46 * to be source-compatible with <code>java.awt.TextField</code>
47 * used with <code>echoChar</code> set. It is provided separately
48 * to make it easier to safely change the UI for the
49 * <code>JTextField</code> without affecting password entries.
50 * <p>
51 * <strong>NOTE:</strong>
52 * By default, JPasswordField disables input methods; otherwise, input
53 * characters could be visible while they were composed using input methods.
54 * If an application needs the input methods support, please use the
55 * inherited method, <code>enableInputMethods(true)</code>.
56 * <p>
57 * <strong>Warning:</strong> Swing is not thread safe. For more
58 * information see <a
59 * href="package-summary.html#threading">Swing's Threading
60 * Policy</a>.
61 * <p>
62 * <strong>Warning:</strong>
63 * Serialized objects of this class will not be compatible with
64 * future Swing releases. The current serialization support is
65 * appropriate for short term storage or RMI between applications running
66 * the same version of Swing. As of 1.4, support for long term storage
67 * of all JavaBeans™
68 * has been added to the <code>java.beans</code> package.
69 * Please see {@link java.beans.XMLEncoder}.
70 *
71 * @beaninfo
72 * attribute: isContainer false
73 * description: Allows the editing of a line of text but doesn't show the characters.
74 *
75 * @author Timothy Prinzing
76 * @since 1.2
77 */
78 @SuppressWarnings("serial") // Same-version serialization only
79 public class JPasswordField extends JTextField {
80
81 /**
82 * Constructs a new <code>JPasswordField</code>,
83 * with a default document, <code>null</code> starting
84 * text string, and 0 column width.
85 */
86 public JPasswordField() {
87 this(null,null,0);
88 }
89
90 /**
91 * Constructs a new <code>JPasswordField</code> initialized
92 * with the specified text. The document model is set to the
93 * default, and the number of columns to 0.
94 *
95 * @param text the text to be displayed, <code>null</code> if none
96 */
97 public JPasswordField(String text) {
134 * @param columns the number of columns to use to calculate
135 * the preferred width >= 0; if columns is set to zero, the
136 * preferred width will be whatever naturally results from
137 * the component implementation
138 */
139 public JPasswordField(Document doc, String txt, int columns) {
140 super(doc, txt, columns);
141 // We could either leave this on, which wouldn't be secure,
142 // or obscure the composted text, which essentially makes displaying
143 // it useless. Therefore, we turn off input methods.
144 enableInputMethods(false);
145 }
146
147 /**
148 * Returns the name of the L&F class that renders this component.
149 *
150 * @return the string "PasswordFieldUI"
151 * @see JComponent#getUIClassID
152 * @see UIDefaults#getUI
153 */
154 public String getUIClassID() {
155 return uiClassID;
156 }
157
158
159 /**
160 * {@inheritDoc}
161 * @since 1.6
162 */
163 public void updateUI() {
164 if(!echoCharSet) {
165 echoChar = '*';
166 }
167 super.updateUI();
168 }
169
170 /**
171 * Returns the character to be used for echoing. The default is '*'.
172 * The default may be different depending on the currently running Look
173 * and Feel. For example, Metal/Ocean's default is a bullet character.
174 *
175 * @return the echo character, 0 if unset
176 * @see #setEchoChar
177 * @see #echoCharIsSet
178 */
179 public char getEchoChar() {
180 return echoChar;
181 }
182
183 /**
184 * Sets the echo character for this <code>JPasswordField</code>.
185 * Note that this is largely a suggestion, since the
186 * view that gets installed can use whatever graphic techniques
187 * it desires to represent the field. Setting a value of 0 indicates
188 * that you wish to see the text as it is typed, similar to
189 * the behavior of a standard <code>JTextField</code>.
190 *
191 * @param c the echo character to display
192 * @see #echoCharIsSet
193 * @see #getEchoChar
194 * @beaninfo
195 * description: character to display in place of the real characters
196 * attribute: visualUpdate true
197 */
198 public void setEchoChar(char c) {
199 echoChar = c;
200 echoCharSet = true;
201 repaint();
202 revalidate();
203 }
204
205 /**
206 * Returns true if this <code>JPasswordField</code> has a character
207 * set for echoing. A character is considered to be set if the echo
208 * character is not 0.
209 *
210 * @return true if a character is set for echoing
211 * @see #setEchoChar
212 * @see #getEchoChar
213 */
214 public boolean echoCharIsSet() {
215 return echoChar != 0;
216 }
217
275 * replaced by <code>getPassword</code>.
276 * @param offs the offset >= 0
277 * @param len the length >= 0
278 * @return the text
279 * @exception BadLocationException if the offset or length are invalid
280 */
281 @Deprecated
282 public String getText(int offs, int len) throws BadLocationException {
283 return super.getText(offs, len);
284 }
285
286 /**
287 * Returns the text contained in this <code>TextComponent</code>.
288 * If the underlying document is <code>null</code>, will give a
289 * <code>NullPointerException</code>. For stronger
290 * security, it is recommended that the returned character array be
291 * cleared after use by setting each character to zero.
292 *
293 * @return the text
294 */
295 public char[] getPassword() {
296 Document doc = getDocument();
297 Segment txt = new Segment();
298 try {
299 doc.getText(0, doc.getLength(), txt); // use the non-String API
300 } catch (BadLocationException e) {
301 return null;
302 }
303 char[] retValue = new char[txt.count];
304 System.arraycopy(txt.array, txt.offset, retValue, 0, txt.count);
305 return retValue;
306 }
307
308 /**
309 * See readObject() and writeObject() in JComponent for more
310 * information about serialization in Swing.
311 */
312 private void writeObject(ObjectOutputStream s) throws IOException {
313 s.defaultWriteObject();
314 if (getUIClassID().equals(uiClassID)) {
366 return false;
367 }
368
369 /////////////////
370 // Accessibility support
371 ////////////////
372
373
374 /**
375 * Returns the <code>AccessibleContext</code> associated with this
376 * <code>JPasswordField</code>. For password fields, the
377 * <code>AccessibleContext</code> takes the form of an
378 * <code>AccessibleJPasswordField</code>.
379 * A new <code>AccessibleJPasswordField</code> instance is created
380 * if necessary.
381 *
382 * @return an <code>AccessibleJPasswordField</code> that serves as the
383 * <code>AccessibleContext</code> of this
384 * <code>JPasswordField</code>
385 */
386 public AccessibleContext getAccessibleContext() {
387 if (accessibleContext == null) {
388 accessibleContext = new AccessibleJPasswordField();
389 }
390 return accessibleContext;
391 }
392
393 /**
394 * This class implements accessibility support for the
395 * <code>JPasswordField</code> class. It provides an implementation of the
396 * Java Accessibility API appropriate to password field user-interface
397 * elements.
398 * <p>
399 * <strong>Warning:</strong>
400 * Serialized objects of this class will not be compatible with
401 * future Swing releases. The current serialization support is
402 * appropriate for short term storage or RMI between applications running
403 * the same version of Swing. As of 1.4, support for long term storage
404 * of all JavaBeans™
405 * has been added to the <code>java.beans</code> package.
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package javax.swing;
26
27 import javax.swing.text.*;
28 import javax.accessibility.*;
29
30 import java.beans.JavaBean;
31 import java.beans.BeanProperty;
32 import java.io.ObjectOutputStream;
33 import java.io.IOException;
34 import java.util.Arrays;
35
36 /**
37 * <code>JPasswordField</code> is a lightweight component that allows
38 * the editing of a single line of text where the view indicates
39 * something was typed, but does not show the original characters.
40 * You can find further information and examples in
41 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html">How to Use Text Fields</a>,
42 * a section in <em>The Java Tutorial.</em>
43 * <p>
44 * <code>JPasswordField</code> is intended
45 * to be source-compatible with <code>java.awt.TextField</code>
46 * used with <code>echoChar</code> set. It is provided separately
47 * to make it easier to safely change the UI for the
48 * <code>JTextField</code> without affecting password entries.
49 * <p>
50 * <strong>NOTE:</strong>
51 * By default, JPasswordField disables input methods; otherwise, input
52 * characters could be visible while they were composed using input methods.
53 * If an application needs the input methods support, please use the
54 * inherited method, <code>enableInputMethods(true)</code>.
55 * <p>
56 * <strong>Warning:</strong> Swing is not thread safe. For more
57 * information see <a
58 * href="package-summary.html#threading">Swing's Threading
59 * Policy</a>.
60 * <p>
61 * <strong>Warning:</strong>
62 * Serialized objects of this class will not be compatible with
63 * future Swing releases. The current serialization support is
64 * appropriate for short term storage or RMI between applications running
65 * the same version of Swing. As of 1.4, support for long term storage
66 * of all JavaBeans™
67 * has been added to the <code>java.beans</code> package.
68 * Please see {@link java.beans.XMLEncoder}.
69 *
70 * @author Timothy Prinzing
71 * @since 1.2
72 */
73 @JavaBean(description = "Allows the editing of a line of text but doesn't show the characters.")
74 @SwingContainer(false)
75 @SuppressWarnings("serial") // Same-version serialization only
76 public class JPasswordField extends JTextField {
77
78 /**
79 * Constructs a new <code>JPasswordField</code>,
80 * with a default document, <code>null</code> starting
81 * text string, and 0 column width.
82 */
83 public JPasswordField() {
84 this(null,null,0);
85 }
86
87 /**
88 * Constructs a new <code>JPasswordField</code> initialized
89 * with the specified text. The document model is set to the
90 * default, and the number of columns to 0.
91 *
92 * @param text the text to be displayed, <code>null</code> if none
93 */
94 public JPasswordField(String text) {
131 * @param columns the number of columns to use to calculate
132 * the preferred width >= 0; if columns is set to zero, the
133 * preferred width will be whatever naturally results from
134 * the component implementation
135 */
136 public JPasswordField(Document doc, String txt, int columns) {
137 super(doc, txt, columns);
138 // We could either leave this on, which wouldn't be secure,
139 // or obscure the composted text, which essentially makes displaying
140 // it useless. Therefore, we turn off input methods.
141 enableInputMethods(false);
142 }
143
144 /**
145 * Returns the name of the L&F class that renders this component.
146 *
147 * @return the string "PasswordFieldUI"
148 * @see JComponent#getUIClassID
149 * @see UIDefaults#getUI
150 */
151 @BeanProperty(bound = false)
152 public String getUIClassID() {
153 return uiClassID;
154 }
155
156
157 /**
158 * {@inheritDoc}
159 * @since 1.6
160 */
161 public void updateUI() {
162 if(!echoCharSet) {
163 echoChar = '*';
164 }
165 super.updateUI();
166 }
167
168 /**
169 * Returns the character to be used for echoing. The default is '*'.
170 * The default may be different depending on the currently running Look
171 * and Feel. For example, Metal/Ocean's default is a bullet character.
172 *
173 * @return the echo character, 0 if unset
174 * @see #setEchoChar
175 * @see #echoCharIsSet
176 */
177 public char getEchoChar() {
178 return echoChar;
179 }
180
181 /**
182 * Sets the echo character for this <code>JPasswordField</code>.
183 * Note that this is largely a suggestion, since the
184 * view that gets installed can use whatever graphic techniques
185 * it desires to represent the field. Setting a value of 0 indicates
186 * that you wish to see the text as it is typed, similar to
187 * the behavior of a standard <code>JTextField</code>.
188 *
189 * @param c the echo character to display
190 * @see #echoCharIsSet
191 * @see #getEchoChar
192 */
193 @BeanProperty(bound = false, visualUpdate = true, description
194 = "character to display in place of the real characters")
195 public void setEchoChar(char c) {
196 echoChar = c;
197 echoCharSet = true;
198 repaint();
199 revalidate();
200 }
201
202 /**
203 * Returns true if this <code>JPasswordField</code> has a character
204 * set for echoing. A character is considered to be set if the echo
205 * character is not 0.
206 *
207 * @return true if a character is set for echoing
208 * @see #setEchoChar
209 * @see #getEchoChar
210 */
211 public boolean echoCharIsSet() {
212 return echoChar != 0;
213 }
214
272 * replaced by <code>getPassword</code>.
273 * @param offs the offset >= 0
274 * @param len the length >= 0
275 * @return the text
276 * @exception BadLocationException if the offset or length are invalid
277 */
278 @Deprecated
279 public String getText(int offs, int len) throws BadLocationException {
280 return super.getText(offs, len);
281 }
282
283 /**
284 * Returns the text contained in this <code>TextComponent</code>.
285 * If the underlying document is <code>null</code>, will give a
286 * <code>NullPointerException</code>. For stronger
287 * security, it is recommended that the returned character array be
288 * cleared after use by setting each character to zero.
289 *
290 * @return the text
291 */
292 @BeanProperty(bound = false)
293 public char[] getPassword() {
294 Document doc = getDocument();
295 Segment txt = new Segment();
296 try {
297 doc.getText(0, doc.getLength(), txt); // use the non-String API
298 } catch (BadLocationException e) {
299 return null;
300 }
301 char[] retValue = new char[txt.count];
302 System.arraycopy(txt.array, txt.offset, retValue, 0, txt.count);
303 return retValue;
304 }
305
306 /**
307 * See readObject() and writeObject() in JComponent for more
308 * information about serialization in Swing.
309 */
310 private void writeObject(ObjectOutputStream s) throws IOException {
311 s.defaultWriteObject();
312 if (getUIClassID().equals(uiClassID)) {
364 return false;
365 }
366
367 /////////////////
368 // Accessibility support
369 ////////////////
370
371
372 /**
373 * Returns the <code>AccessibleContext</code> associated with this
374 * <code>JPasswordField</code>. For password fields, the
375 * <code>AccessibleContext</code> takes the form of an
376 * <code>AccessibleJPasswordField</code>.
377 * A new <code>AccessibleJPasswordField</code> instance is created
378 * if necessary.
379 *
380 * @return an <code>AccessibleJPasswordField</code> that serves as the
381 * <code>AccessibleContext</code> of this
382 * <code>JPasswordField</code>
383 */
384 @BeanProperty(bound = false)
385 public AccessibleContext getAccessibleContext() {
386 if (accessibleContext == null) {
387 accessibleContext = new AccessibleJPasswordField();
388 }
389 return accessibleContext;
390 }
391
392 /**
393 * This class implements accessibility support for the
394 * <code>JPasswordField</code> class. It provides an implementation of the
395 * Java Accessibility API appropriate to password field user-interface
396 * elements.
397 * <p>
398 * <strong>Warning:</strong>
399 * Serialized objects of this class will not be compatible with
400 * future Swing releases. The current serialization support is
401 * appropriate for short term storage or RMI between applications running
402 * the same version of Swing. As of 1.4, support for long term storage
403 * of all JavaBeans™
404 * has been added to the <code>java.beans</code> package.
|