181 public TextField(int columns) throws HeadlessException {
182 this("", columns);
183 }
184
185 /**
186 * Constructs a new text field initialized with the specified text
187 * to be displayed, and wide enough to hold the specified
188 * number of columns. A column is an approximate average character
189 * width that is platform-dependent.
190 * @param text the text to be displayed. If
191 * <code>text</code> is <code>null</code>, the empty
192 * string <code>""</code> will be displayed.
193 * @param columns the number of columns. If
194 * <code>columns</code> is less than <code>0</code>,
195 * <code>columns</code> is set to <code>0</code>.
196 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
197 * returns true.
198 * @see java.awt.GraphicsEnvironment#isHeadless
199 */
200 public TextField(String text, int columns) throws HeadlessException {
201 super(text);
202 this.columns = (columns >= 0) ? columns : 0;
203 }
204
205 /**
206 * Construct a name for this component. Called by getName() when the
207 * name is null.
208 */
209 String constructComponentName() {
210 synchronized (TextField.class) {
211 return base + nameCounter++;
212 }
213 }
214
215 /**
216 * Creates the TextField's peer. The peer allows us to modify the
217 * appearance of the TextField without changing its functionality.
218 */
219 public void addNotify() {
220 synchronized (getTreeLock()) {
221 if (peer == null)
280 * replaced by <code>setEchoChar(char)</code>.
281 */
282 @Deprecated
283 public synchronized void setEchoCharacter(char c) {
284 if (echoChar != c) {
285 echoChar = c;
286 TextFieldPeer peer = (TextFieldPeer)this.peer;
287 if (peer != null) {
288 peer.setEchoChar(c);
289 }
290 }
291 }
292
293 /**
294 * Sets the text that is presented by this
295 * text component to be the specified text.
296 * @param t the new text.
297 * @see java.awt.TextComponent#getText
298 */
299 public void setText(String t) {
300 super.setText(t);
301
302 // This could change the preferred size of the Component.
303 invalidateIfValid();
304 }
305
306 /**
307 * Indicates whether or not this text field has a
308 * character set for echoing.
309 * <p>
310 * An echo character is useful for text fields where
311 * user input should not be echoed to the screen, as in
312 * the case of a text field for entering a password.
313 * @return <code>true</code> if this text field has
314 * a character set for echoing;
315 * <code>false</code> otherwise.
316 * @see java.awt.TextField#setEchoChar
317 * @see java.awt.TextField#getEchoChar
318 */
319 public boolean echoCharIsSet() {
320 return echoChar != 0;
321 }
322
323 /**
324 * Gets the number of columns in this text field. A column is an
325 * approximate average character width that is platform-dependent.
326 * @return the number of columns.
687 }
688
689 /**
690 * Read the ObjectInputStream and if it isn't null,
691 * add a listener to receive action events fired by the
692 * TextField. Unrecognized keys or values will be
693 * ignored.
694 *
695 * @exception HeadlessException if
696 * <code>GraphicsEnvironment.isHeadless()</code> returns
697 * <code>true</code>
698 * @see #removeActionListener(ActionListener)
699 * @see #addActionListener(ActionListener)
700 * @see java.awt.GraphicsEnvironment#isHeadless
701 */
702 private void readObject(ObjectInputStream s)
703 throws ClassNotFoundException, IOException, HeadlessException
704 {
705 // HeadlessException will be thrown by TextComponent's readObject
706 s.defaultReadObject();
707
708 // Make sure the state we just read in for columns has legal values
709 if (columns < 0) {
710 columns = 0;
711 }
712
713 // Read in listeners, if any
714 Object keyOrNull;
715 while(null != (keyOrNull = s.readObject())) {
716 String key = ((String)keyOrNull).intern();
717
718 if (actionListenerK == key) {
719 addActionListener((ActionListener)(s.readObject()));
720 } else {
721 // skip value for unrecognized key
722 s.readObject();
723 }
724 }
725 }
726
|
181 public TextField(int columns) throws HeadlessException {
182 this("", columns);
183 }
184
185 /**
186 * Constructs a new text field initialized with the specified text
187 * to be displayed, and wide enough to hold the specified
188 * number of columns. A column is an approximate average character
189 * width that is platform-dependent.
190 * @param text the text to be displayed. If
191 * <code>text</code> is <code>null</code>, the empty
192 * string <code>""</code> will be displayed.
193 * @param columns the number of columns. If
194 * <code>columns</code> is less than <code>0</code>,
195 * <code>columns</code> is set to <code>0</code>.
196 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
197 * returns true.
198 * @see java.awt.GraphicsEnvironment#isHeadless
199 */
200 public TextField(String text, int columns) throws HeadlessException {
201 super(replaceEOL(text));
202 this.columns = (columns >= 0) ? columns : 0;
203 }
204
205 /**
206 * Construct a name for this component. Called by getName() when the
207 * name is null.
208 */
209 String constructComponentName() {
210 synchronized (TextField.class) {
211 return base + nameCounter++;
212 }
213 }
214
215 /**
216 * Creates the TextField's peer. The peer allows us to modify the
217 * appearance of the TextField without changing its functionality.
218 */
219 public void addNotify() {
220 synchronized (getTreeLock()) {
221 if (peer == null)
280 * replaced by <code>setEchoChar(char)</code>.
281 */
282 @Deprecated
283 public synchronized void setEchoCharacter(char c) {
284 if (echoChar != c) {
285 echoChar = c;
286 TextFieldPeer peer = (TextFieldPeer)this.peer;
287 if (peer != null) {
288 peer.setEchoChar(c);
289 }
290 }
291 }
292
293 /**
294 * Sets the text that is presented by this
295 * text component to be the specified text.
296 * @param t the new text.
297 * @see java.awt.TextComponent#getText
298 */
299 public void setText(String t) {
300 super.setText(replaceEOL(t));
301
302 // This could change the preferred size of the Component.
303 invalidateIfValid();
304 }
305
306 /**
307 * Replaces EOL characters from the text variable with a space character.
308 * @param text the new text.
309 * @return Returns text after replacing EOL characters.
310 */
311 private static String replaceEOL(String text) {
312 String[] strEOLs = {System.lineSeparator(), "\n"};
313 for (String eol : strEOLs) {
314 if (text.contains(eol)) {
315 text = text.replace(eol, " ");
316 }
317 }
318 return text;
319 }
320
321
322 /**
323 * Indicates whether or not this text field has a
324 * character set for echoing.
325 * <p>
326 * An echo character is useful for text fields where
327 * user input should not be echoed to the screen, as in
328 * the case of a text field for entering a password.
329 * @return <code>true</code> if this text field has
330 * a character set for echoing;
331 * <code>false</code> otherwise.
332 * @see java.awt.TextField#setEchoChar
333 * @see java.awt.TextField#getEchoChar
334 */
335 public boolean echoCharIsSet() {
336 return echoChar != 0;
337 }
338
339 /**
340 * Gets the number of columns in this text field. A column is an
341 * approximate average character width that is platform-dependent.
342 * @return the number of columns.
703 }
704
705 /**
706 * Read the ObjectInputStream and if it isn't null,
707 * add a listener to receive action events fired by the
708 * TextField. Unrecognized keys or values will be
709 * ignored.
710 *
711 * @exception HeadlessException if
712 * <code>GraphicsEnvironment.isHeadless()</code> returns
713 * <code>true</code>
714 * @see #removeActionListener(ActionListener)
715 * @see #addActionListener(ActionListener)
716 * @see java.awt.GraphicsEnvironment#isHeadless
717 */
718 private void readObject(ObjectInputStream s)
719 throws ClassNotFoundException, IOException, HeadlessException
720 {
721 // HeadlessException will be thrown by TextComponent's readObject
722 s.defaultReadObject();
723 text = replaceEOL(text);
724
725 // Make sure the state we just read in for columns has legal values
726 if (columns < 0) {
727 columns = 0;
728 }
729
730 // Read in listeners, if any
731 Object keyOrNull;
732 while(null != (keyOrNull = s.readObject())) {
733 String key = ((String)keyOrNull).intern();
734
735 if (actionListenerK == key) {
736 addActionListener((ActionListener)(s.readObject()));
737 } else {
738 // skip value for unrecognized key
739 s.readObject();
740 }
741 }
742 }
743
|