Using the TextPaneComposite Class on a Shell

Composite objects themselves cannot be drawn directly on the display, but must be placed within the confines of a Shell or Dialog, the two classes that can be drawn directly on the display.

How do I do that?

Create an instance of the TextPaneComposite class, passing it a reference to the Shell instance that serves as its container. Example 10-2 demonstrates how to use TextPaneComposite on a Shell.

Example 10-2. Using a Composite subclass on a Shell

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class CompositeShellExample {
    Display d;
    Shell s;
    CompositeShellExample( )    {
        d = new Display( );
        s = new Shell(d);
        s.setSize(500,500);
        s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
        s.setText("A Shell Composite Example");
        s.setLayout(new FillLayout( ));
        final TextPaneComposite tpc = new TextPaneComposite(s);
        s.open( );
        while(!s.isDisposed( )){
            if(!d.readAndDispatch( ))
                d.sleep( );
        }
        d.dispose( );
    }
}

In this class, you see code that is very familiar from the previous examples, with one difference—instead of creating a Text widget directly on the Shell workspace, you create an instance of TextPaneComposite, passing it a reference to the Shell instance which will be the parent. All other code remains the same. The results are shown in Figure 10-1.

Figure 10-1. A Composite with a Text widget on a Shell

Although this is a pretty simple 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.