java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4091
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to validate SWT Text Controls E-mail
User Rating: / 62
PoorBest 

Sometimes it needs to have the input of the Text field in fixed format. Like you want input only in numeric form or in Date form. In other words, the Text field may need input with some restrictions. It can be done using the verify listeners of the Text Widgets.

Letís take an example of the Text field that accepts only numeric data:

1. Text textNumber=new Text(parent,SWT.BORDER);
2. textNumber.addListener(SWT.Verify, new Listener() {
3.           public void handleEvent(Event e) {
4.              String string = e.text;
5.              char[] chars = new char[string.length()];
6.              string.getChars(0, chars.length, chars, 0);
7.              for (int i = 0; i < chars.length; i++) {
8.                if (!('0' <= chars[i&& chars[i<= '9')) {
9.                  e.doit = false;
10.                return;
11.               }
12.              }
13.            }
14.         });

In the above code first a Text field is created (In step 1). Then verify listener is added to the Text (In step 2). In the handleEvent() method of the verify listener you can write your own logic of formatting the Text input, like in the above code it checks whether the current input is numeric or not(In step 8).

So when you try to give input to the Text, First it checks the input format. If it satisfies the condition (as given in line 8) then input will be accepted otherwise would not accept it (In step 9).

In the same way Text for Date format or any special format can be created.


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.