Creating Display-Only Text Fields

At times you may need to display text to the user without permitting the user to modify or otherwise interact with the text—in effect, creating a display-only text field.

How do I do that?

To create a read-only text field, set the text field’s enabled attribute to false. This creates a text field that is grayed out as in Figure 5-8.

Disabled text field

Figure 5-8. Disabled text field

The user may see the text, but may not interact with it in any way. Often, these types of field are referred to as being disabled :

text1.setEnabled(false);

What about...

times when you want to prevent the user from editing the text field, but still want to allow the user to select text for copying and pasting to another field or another application? Disabling a text field is not appropriate if you must enable the user to interact with the text. For this task, you use the setEditable() method:

text1.setEditable(false);

This results in a text field that behaves normally in all respects except that the user is not permitted to alter the text.

Note

Text fields that aren’t editable are grayed out.

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.