by Dinesh Thakur Category: AWT and Applets

A Textfield is a component used for displaying, inputting and editing a single line of plain text. We can create textfield by creating an instance of Textfield class. The TextComponent is a superclass of TextField that provides common set of methods used by Textfield.

Text Classes User Interface

 

Method

Returns

Notes

getSelectedText()

String

 The string returned is only the selected text

getSelectionEnd()

Int

The index to the last character selected

getSelectionStart()

Int

The index to the first character selected

getText()

String

The entire contents of the text object

isEditable()

Boolean

if true, the user can type in the text object

select(int selStart, int selEnd)

Void

Use this to “pre-mark” text(as in search)

selectAll()

Void

Mark the whole text object contents(as in a file dialog window)

setEditable(boolean t)

Void

if true, the user can type in the object

setText(String t)

Void

Replace the entire contents of the text object

TextField()

 

Columns are approximate, some windowing systems don’t seem “perfect”

TextField(int cols)

 

 

TextField(String text)

 

 

TextField(String text, int cols )

 

 

echoCharIsSet()

Boolean

False if we’re echoing the keystrokes properly, true if we’re echoing a single character(like password echo)

getColumns()

Int

How wide is the textfield?

getEchoChar()

Char

What is the password echo character?

minimumSize()

Dimension

Tell us(in pixels) what the minimum space required is

minimumSize(int cols)

Dimension

Tell us(in pixels) what the minimum space required is

preferredSize()

Dimension

 

preferredSize(int Cols)

Dimension

 

setEchoCharacter(char c)

Void

Set the character to echo on keystrokes(password echo)

 

import java.awt.*;

class Textfieldframe extends Frame
{
      Textfieldframe()
     {
            setLayout(new FlowLayout());
            Label lblUserid = new Label("Userid");
            TextField TxtUserid = new TextField(10);
            Label lblpassword = new Label("password");
            TextField Txtpassword = new TextField("Mr Thakur",13);
            Txtpassword.setEchoChar('*');
            add(lblUserid); add(TxtUserid);
            add(lblpassword); add(Txtpassword);
     }
}
   class TextFieldsJavaExample
   {
         public static void main(String args[])
        {
              Textfieldframe frame = new Textfieldframe();
              frame.setTitle("Text Fields in Java Example");
              frame.setSize(220,150);
              frame.setResizable(false);
              frame.setVisible(true);
        }
   }

Text Fields in Java Example

Popular Article