JTextField & JPasswordField on JPanel

Home  »  Java  »  Advance Java »  »  Swing »  JTextField & JPassword

A text field is a basic text control that lets the user enter a small amount of text. Generally used with GUI based data entry screens in commercial application or Applets.

Text fields are implemented in Swing by the JTextField class.

JTextField class constructors include the following:

  1. JTextField(int) A text field with the specified width.
  2. JTextField(String, int) A text field with the specified text and width.

Example:

The following example creates a text field having a column width of 10 units.



The JPasswordFiled class creates a text field that can use a character to obscure input.
The JPasswordField class is a subclass of JTextField class.

This class has constructor methods similar to the constructors of the JTextField class.
  1. JPasswordField(imt)The int argument specifies the desired width of the column.
  2. JPasswordField(String, int) The string argument contains the fields initial text, and The int argument specifies the desired width of the column.

Once a password text field is created, setEchoChar(char) method can be used to obscure input with the specified character.

The following table describes some of the methods provided by the JTextField class

MethodsDescription
void setEditable(boolean)
boolean isEditable()
Sets of gets whether the user can edit the text in the text field.
void setColumns(int)
int getColumns()
Set or get the number of columns displayed by the text field.
void getColumnWidth()Get the width of the text field's columns.
void setHorizontalAlignment(int)
int getHorizontalAlignment()
Set or get how the text is aligned horizontally within its area. One can use JTextField.LEFT , JTextField.CENTER, and JTextField.RIGHT for aligning text in the text field.
void setEchoChar(char)
char getEchoChar()
(in JPasswordField)
Set or get the echo character -- the character displayed instead of the characters typed by the user.


The following example creates an object of the JPasswordField class and sets the echo character as a Bintu symbol



Example:

The following example displayed one text field and one password field.



C:\>jdk1.4\bin>javac PassDemo.java
C:\>jdk1.4\bin>java PassDemo

"Output of PassDemo.class"


Download Complete Program

  -   >



Java Swing-1


JFrame JPanel JApplet
JButtons JLabels JTextFields
JPasswordField JTextAreas JCheckBoxes
JRadioButtons JList JComboBox




1 comment: