Creating Multiline Text Fields

Often your interface design mandates that you create a text field that permits a user to enter more data than can be conveniently viewed in a single-line edit type of text field. For these types of applications, such as text editors or database applications that allow the entry of free-form text (perhaps for comment fields), you must use a multiline edit-style text field.

How do I do that?

Creating a multiline text field is easy—just specify the SWT.MULTI style in the Text constructor:

Text text1 = new Text(s, SWT.MULTI | SWT.BORDER);

Example 5-3 creates the window shown in Figure 5-4, where you see a multiline text field that fills the entire workspace area of the window.

Example 5-3. A multiline text field

public class TextFieldExample {
    
    Display d;
    Shell s;
    TextFieldExample( )    {
        d = new Display( );
        s = new Shell(d);
        s.setSize(250,250);
        s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
        s.setText("A Text Field Example");
        Text text1 = new Text(s, SWT.MULTI | SWT.BORDER);
        text1.setBounds(0,0,250,250);
        s.open( );
        while(!s.isDisposed( )){
            if(!d.readAndDispatch( ))
                d.sleep( );
        }
        d.dispose( );
    }
}
A multiline example

Figure 5-4. A multiline example

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.