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.