< 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      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 163      * returns true.
 164      * @see java.awt.GraphicsEnvironment#isHeadless
 165      */
 166     public TextField(String text) throws HeadlessException {
 167         this(text, (text != null) ? text.length() : 0);
 168     }
 169 
 170     /**
 171      * Constructs a new empty text field with the specified number
 172      * of columns.  A column is an approximate average character
 173      * width that is platform-dependent.
 174      * @param      columns     the number of columns.  If
 175      *             {@code columns} is less than {@code 0},
 176      *             {@code columns} is set to {@code 0}.
 177      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
 178      * returns true.
 179      * @see java.awt.GraphicsEnvironment#isHeadless
 180      */
 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} is {@code null}, the empty
 192      *             string {@code ""} will be displayed.


 193      * @param      columns     the number of columns.  If
 194      *             {@code columns} is less than {@code 0},
 195      *             {@code columns} is set to {@code 0}.
 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         }


 276      *
 277      *  @param  c the echo character for this text field
 278      *
 279      * @deprecated As of JDK version 1.1,
 280      * replaced by {@code setEchoChar(char)}.
 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         if (text == null) {
 313             return text;
 314         }
 315         String[] strEOLs = {System.lineSeparator(), "\n"};
 316         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 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         }


 280      *
 281      *  @param  c the echo character for this text field
 282      *
 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) {


< prev index next >