Creating TextFields & Password in an Applet Window using Advanced Java Programming

Home  »  Java  »  Advance Java »    Java AWT (Abstract Window ToolKit) »    TextFields



Text fields provide an area value where on can enter and edit a single line of text. Text fields are generally used for getting text input from a user.

One can use the TextField class to create text fields in a container, by using one of the manager.

  1. TextField() Empty TextBox which will never display any Text String into that TextBox and this looks as default Text Field neither a Text nor a Specified Length of TextBox.
  2. TextField(int) This will Create a TextField and this will Accept the Number which will Specify Size of the textbox. Size never Means as Length as total Number of characters rather this determines the number of characters to be displayed at a Time I the textbox.
  3. TextField(String) Creates a text field with a given string.
  4. TextField(Strings, int) Creates a text field with the name of strings and the site of textbox which contains a Given String and When you Exceeds the number of Characters of a TextBox then you have to use the Arrow Keys for displaying Previous Text from a TextField. All the Examples of a TextField as follows:-

TextField ti=new TextField();
TextField t2=new TextField(20);
TextField t3=new TextField("Hello", 10);

The following program code show how to create a frame window:

The above code will be stored in the file called TextFieldsTest.java, which on compilation will crate a class file called TextFieldsTest.class. Now compiple this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac TextFieldsTest.java
C:\>jdk1.4\bin>appletviewer TextFieldsTest.java

"Output of TextFieldTest.java"


Download Complete Program

  -  



Methods of the TextFields Object:

Methods
Action
setText(String) used to set the text in the TextBox.
getText() Used to get the Text from a TextField.
setColumns() Used for Specifying how many Columns a user wan1ts o display the text in the TextField.
Select (int, int) select the text from first point to last point.
selectAll() This will Select text from a TextBox.
isEditable() return true or false based on whether the text is Editable. Or whether a user ca write any text into a TextField.
setEditable(Boolean) Used to set the State for Specifying whether we can write in the TextField or not
getEchoChar(char) it returns the character used for masking input.
setEchochar(char)This is used to set the Character Format as a Input.
echoCharItSet() Return true or false based on whether the field has Echo character or no.




Creating Password


You can create a password field using the methods of the TextField class. The following program code shows how to create password fields:


The above code will be stored in the file called TextFieldsPassword.java, which on compilation will crate a class file called TextFieldsPassword.class. Now compiple this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac TextFieldsPassword.java
C:\>jdk1.4\bin>appletviewer TextFieldsPassword.java

"Output of TextFieldPassword.java"


Download Complete Program

Java Source Code   -   Class File






AWT Controls



Label Buttons Check Boxes Radio Buttons
Choice Controls TextFields & Password TextAreas Scrolling Lists Scrollbars



1 comment: