< prev index next >

src/java.desktop/share/classes/java/awt/TextField.java

Print this page




 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.




 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         if (text == null) {
 313             return text;
 314         }
 315         String[] strEOLs = {System.lineSeparator(), "\n"};
 316         for (String eol : strEOLs) {
 317             if (text.contains(eol)) {
 318                 text = text.replace(eol, " ");
 319             }
 320         }
 321         return text;
 322     }
 323 
 324 
 325     /**
 326      * Indicates whether or not this text field has a
 327      * character set for echoing.
 328      * <p>
 329      * An echo character is useful for text fields where
 330      * user input should not be echoed to the screen, as in
 331      * the case of a text field for entering a password.
 332      * @return     <code>true</code> if this text field has
 333      *                 a character set for echoing;
 334      *                 <code>false</code> otherwise.


< prev index next >