Home arrow Java & J2EE arrow Page 9 - Java's Basic User Interface Components

Setting a Password Character - Java

To simplify user interaction and make data entry easier, you can use java controls. Controls are components, such as buttons, labels and text boxes, that can be added to containers like frames, panels and applets. The Java.awt package provides an integrated set of classes to manage user interface components.

TABLE OF CONTENTS:
  1. Java's Basic User Interface Components
  2. Adding a Control to a Container
  3. The Button Class
  4. The Label Class
  5. The Checkbox Class
  6. The CheckboxGroup Class
  7. The Choice List Class
  8. The TextField and TextArea Class
  9. Setting a Password Character
  10. TextArea
By: Gayathri Gokul
Rating: starstarstarstarstar / 22
October 20, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
The text Field allows you to specify an echo character that helps you accept a password without displaying what the user has keyed in. For example, say you have specified “*” as the echo character, and the user type in five character, “*****” is displayed in the text filed instead of the text the user has keyed in. To do this, first create the text field itself; then use the setEchoCharacter() method to set the character that is echoed on the screen. Here is an example:

TextField tf = new TextField(30);tf.setEchoCharacter('*');

The Applet in Example7 below creates several text fields. Labels are used to identify the fields. One of the fields uses an obscuring character to hide text being input. This applet uses the default layout manager, the only thing causing the six components to appear in three different lines is the width of the window.

/*
<Applet code= “TextfieldTest.class”
Width = 400
Height = 150>
</Applet>
*/

Import java.awt.*;
Public class TextfieldTest extends java.applet.Applet
{
Label uLabel = new label (“User Name:”);
TextField uText = new TextField (25);
Label eLabel = new label (“Email ID:”);
TextField eText = new TextField (25);
Label pLabel = new label (“Admin Password:”);
TextField pText = new TextField(25);

Public void init(){
add(uLabel);
add(uText);
add(eLabel);
add(eText);
add(pLabel);
pText.setEchoCharacter(‘*’);
add(pText);
}
}


After creating the controls, we initialize them using the “new” keyword. Use the add() method to add controls to the container, in this case the applet. The other methods for TextField are:

Method Action
String getText() Extracts the text from the text field (as a string).
Void setText(String str)Sets the text of the text field to the text specified in the str.
Int getColumns()Returns the width of this text field.
select(int, int) Selects the text between the two integer positions (positions start from 0)
selectAll() Selects all the text in the field
Boolean isEditable ()Returns true or false based on whether the text is editable
Char getEchoChar()Returns the character used for masking input
Boolean echoCharIsSet(char c) Returns true or false based on whether the field has a masking character


 
 
>>> More Java & J2EE Articles          >>> More By Gayathri Gokul
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: