Interacting with the Text in the Text Widget
If you are building an interface that displays text to the user from some other source—in other words, you’re using text fields that are populated with text values when the window is opened or data is retrieved—you must directly interact with the contents of the text field.
How do I do that?
There are two possible interactions with the text in a
field—
reading the text out of the field
and pushing text into the field. The SWT widgets use the
getXXX( )
and setXXX( )
methods similar to the JavaBeans Specification.
A call to the setText()
method
of the Text
class causes a text value to appear:
text1.setText("Some text goes here");
Warning
You can actually set text here that exceeds the character limit
imposed by setTextLimit( )
. No harm will occur if
you do so, but if you later attempt to take the value in that text
field and put it into a database column that would accept text only
up to the limit, an error would occur at that point. Care must be
taken to ensure that the character limit is not exceeded when you
programmatically enter text in this manner.
The converse operation is to read text out of a text field and store
it in a variable for programmatic use. For that task, use the
getText( )
method:
String contents = text1.getText( );
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.