< prev index next >

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

Print this page




 142         if (!GraphicsEnvironment.isHeadless()) {
 143             initIDs();
 144         }
 145     }
 146 
 147     /**
 148      * Constructs a new text field.
 149      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 150      * returns true.
 151      * @see java.awt.GraphicsEnvironment#isHeadless
 152      */
 153     public TextField() throws HeadlessException {
 154         this("", 0);
 155     }
 156 
 157     /**
 158      * Constructs a new text field initialized with the specified text.
 159      * @param      text       the text to be displayed. If
 160      *             {@code text} is {@code null}, the empty
 161      *             string {@code ""} will be displayed.
 162      *             If {@code text} contains EOL character, then
 163      *             it will be replaced by space character.
 164      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 165      * returns true.
 166      * @see java.awt.GraphicsEnvironment#isHeadless
 167      */
 168     public TextField(String text) throws HeadlessException {
 169         this(text, (text != null) ? text.length() : 0);
 170     }
 171 
 172     /**
 173      * Constructs a new empty text field with the specified number
 174      * of columns.  A column is an approximate average character
 175      * width that is platform-dependent.
 176      * @param      columns     the number of columns.  If
 177      *             {@code columns} is less than {@code 0},
 178      *             {@code columns} is set to {@code 0}.
 179      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 180      * returns true.
 181      * @see java.awt.GraphicsEnvironment#isHeadless
 182      */
 183     public TextField(int columns) throws HeadlessException {
 184         this("", columns);
 185     }
 186 
 187     /**
 188      * Constructs a new text field initialized with the specified text
 189      * to be displayed, and wide enough to hold the specified
 190      * number of columns. A column is an approximate average character
 191      * width that is platform-dependent.
 192      * @param      text       the text to be displayed. If
 193      *             {@code text} is {@code null}, the empty
 194      *             string {@code ""} will be displayed.
 195      *             If {@code text} contains EOL character, then
 196      *             it will be replaced by space character.
 197      * @param      columns     the number of columns.  If
 198      *             {@code columns} is less than {@code 0},
 199      *             {@code columns} is set to {@code 0}.
 200      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 201      * returns true.
 202      * @see java.awt.GraphicsEnvironment#isHeadless
 203      */
 204     public TextField(String text, int columns) throws HeadlessException {
 205         super(replaceEOL(text));
 206         this.columns = (columns >= 0) ? columns : 0;
 207     }
 208 
 209     /**
 210      * Construct a name for this component.  Called by getName() when the
 211      * name is null.
 212      */
 213     String constructComponentName() {
 214         synchronized (TextField.class) {
 215             return base + nameCounter++;
 216         }


 283      * @deprecated As of JDK version 1.1,
 284      * replaced by {@code setEchoChar(char)}.
 285      */
 286     @Deprecated
 287     public synchronized void setEchoCharacter(char c) {
 288         if (echoChar != c) {
 289             echoChar = c;
 290             TextFieldPeer peer = (TextFieldPeer)this.peer;
 291             if (peer != null) {
 292                 peer.setEchoChar(c);
 293             }
 294         }
 295     }
 296 
 297     /**
 298      * Sets the text that is presented by this
 299      * text component to be the specified text.
 300      * @param      t       the new text. If
 301      *             {@code t} is {@code null}, the empty
 302      *             string {@code ""} will be displayed.
 303      *             If {@code t} contains EOL character, then
 304      *             it will be replaced by space character.
 305      * @see         java.awt.TextComponent#getText
 306      */
 307     public void setText(String t) {
 308         super.setText(replaceEOL(t));
 309 
 310         // This could change the preferred size of the Component.
 311         invalidateIfValid();
 312     }
 313 
 314     /**
 315      * Replaces EOL characters from the text variable with a space character.
 316      * @param       text   the new text.
 317      * @return      Returns text after replacing EOL characters.
 318      */
 319     private static String replaceEOL(String text) {
 320         if (text == null) {
 321             return text;
 322         }
 323         String[] strEOLs = {System.lineSeparator(), "\n"};
 324         for (String eol : strEOLs) {




 142         if (!GraphicsEnvironment.isHeadless()) {
 143             initIDs();
 144         }
 145     }
 146 
 147     /**
 148      * Constructs a new text field.
 149      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 150      * returns true.
 151      * @see java.awt.GraphicsEnvironment#isHeadless
 152      */
 153     public TextField() throws HeadlessException {
 154         this("", 0);
 155     }
 156 
 157     /**
 158      * Constructs a new text field initialized with the specified text.
 159      * @param      text       the text to be displayed. If
 160      *             {@code text} is {@code null}, the empty
 161      *             string {@code ""} will be displayed.
 162      *             If {@code text} contains EOL and/or LF characters, then
 163      *             each will be replaced by space character.
 164      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 165      * returns true.
 166      * @see java.awt.GraphicsEnvironment#isHeadless
 167      */
 168     public TextField(String text) throws HeadlessException {
 169         this(text, (text != null) ? text.length() : 0);
 170     }
 171 
 172     /**
 173      * Constructs a new empty text field with the specified number
 174      * of columns.  A column is an approximate average character
 175      * width that is platform-dependent.
 176      * @param      columns     the number of columns.  If
 177      *             {@code columns} is less than {@code 0},
 178      *             {@code columns} is set to {@code 0}.
 179      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 180      * returns true.
 181      * @see java.awt.GraphicsEnvironment#isHeadless
 182      */
 183     public TextField(int columns) throws HeadlessException {
 184         this("", columns);
 185     }
 186 
 187     /**
 188      * Constructs a new text field initialized with the specified text
 189      * to be displayed, and wide enough to hold the specified
 190      * number of columns. A column is an approximate average character
 191      * width that is platform-dependent.
 192      * @param      text       the text to be displayed. If
 193      *             {@code text} is {@code null}, the empty
 194      *             string {@code ""} will be displayed.
 195      *             If {@code text} contains EOL and/or LF characters, then
 196      *             each will be replaced by space character.
 197      * @param      columns     the number of columns.  If
 198      *             {@code columns} is less than {@code 0},
 199      *             {@code columns} is set to {@code 0}.
 200      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 201      * returns true.
 202      * @see java.awt.GraphicsEnvironment#isHeadless
 203      */
 204     public TextField(String text, int columns) throws HeadlessException {
 205         super(replaceEOL(text));
 206         this.columns = (columns >= 0) ? columns : 0;
 207     }
 208 
 209     /**
 210      * Construct a name for this component.  Called by getName() when the
 211      * name is null.
 212      */
 213     String constructComponentName() {
 214         synchronized (TextField.class) {
 215             return base + nameCounter++;
 216         }


 283      * @deprecated As of JDK version 1.1,
 284      * replaced by {@code setEchoChar(char)}.
 285      */
 286     @Deprecated
 287     public synchronized void setEchoCharacter(char c) {
 288         if (echoChar != c) {
 289             echoChar = c;
 290             TextFieldPeer peer = (TextFieldPeer)this.peer;
 291             if (peer != null) {
 292                 peer.setEchoChar(c);
 293             }
 294         }
 295     }
 296 
 297     /**
 298      * Sets the text that is presented by this
 299      * text component to be the specified text.
 300      * @param      t       the new text. If
 301      *             {@code t} is {@code null}, the empty
 302      *             string {@code ""} will be displayed.
 303      *             If {@code t} contains EOL and/or LF characters, then
 304      *             each will be replaced by space character.
 305      * @see         java.awt.TextComponent#getText
 306      */
 307     public void setText(String t) {
 308         super.setText(replaceEOL(t));
 309 
 310         // This could change the preferred size of the Component.
 311         invalidateIfValid();
 312     }
 313 
 314     /**
 315      * Replaces EOL characters from the text variable with a space character.
 316      * @param       text   the new text.
 317      * @return      Returns text after replacing EOL characters.
 318      */
 319     private static String replaceEOL(String text) {
 320         if (text == null) {
 321             return text;
 322         }
 323         String[] strEOLs = {System.lineSeparator(), "\n"};
 324         for (String eol : strEOLs) {


< prev index next >