Creating Multiple Instances of a Composite Subclass

Of course, the main reason for choosing to create a Group as part of a Composite subclass is that when you want to create multiple instances of the Group on the same window or to use the Group on multiple windows.

How do I do that?

Create an instance of the subclass for each time you wish it to appear, as shown in Example 10-7.

Example 10-7. Creating multiple instances of a Composite subclass

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

public class GroupShellExample {
    Display d;
    Shell s;
    GroupShellExample( )    {
        d = new Display( );
        s = new Shell(d);
        s.setSize(250,250);
        s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
        s.setText("A Shell Composite Example");
        final GroupExample ge1 = new GroupExample(s, SWT.SHADOW_ETCHED_IN, 
                 "Option Group One" );
        ge1.setLocation(10,10);
        final GroupExample ge2 = new GroupExample(s, SWT.SHADOW_ETCHED_IN, 
           "Option Group Two" );
        ge2.setLocation(100,100);
        s.open( );
        while(!s.isDisposed( )){
            if(!d.readAndDispatch( ))
                d.sleep( );
        }
        d.dispose( );
    }
}

Now, if you create an instance of GroupShellExample, from Example 10-7, you cause Figure 10-3 to be displayed.

Reusing GroupExample

Figure 10-3. Reusing GroupExample

You can create as many instances of a Composite subclass as required for your interface.

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.