Creating Password Fields

Sometimes you may want to create fields that allow text to be entered, but that hide that text from onlookers. A password entry field is a typical example. Because it would not be secure to display the text that is entered for all to see, a specific character is displayed to mask that text as it is being entered.

How do I do that?

To create a password-type field, use the setEchoChar() method:

Note

Notice that you pass a single-quoted character to this method, not a double-quoted string.

text1.setEchoChar('*');

This code results in Figure 5-9.

A password field using setEchoChar( )

Figure 5-9. A password field using setEchoChar( )

As you can see, SWT provides a very flexible widget for display and entry of text, which is an essential element of almost every GUI. Since you’re using SWT, most of the implementation details are hidden from you in the Java classes and native libraries that lie under the hood.

Get SWT: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.