Creating Child Shells

It is often necessary to open windows that appear not on the desktop or window manager, but within another window. One example of this is the ever-popular Multiple Document Interface (MDI) found in earlier versions of applications such as Microsoft Excel. Another example would be a prompt to the user for information or a display of some type of message.

Result of ShellStylesExample

Figure 2-3. Result of ShellStylesExample

Tip

MessageBox and other common forms of dialogs are created from another set of SWT classes and are discussed in Chapter 17.

How do I do that?

To open a window within a window, use a third constructor of the Shell class:

Shell(Shell parent)

You can also use a similar constructor that enables you to specify a style:

Shell(Shell parent, int style)

These constructors behave exactly the same as in the earlier examples, except that they cause the shell being opened to be displayed within the shell that is the parent, instead of displaying directly upon the desktop or the window manager.

Example 2-5 creates an instance of Shell that is suitable for opening within the confines of a parent shell.

Example 2-5. A child shell

import org.eclipse.swt.widgets.*;
public class ChildShell {
    
    
ChildShell(Shell parent
){
        Shell child = new Shell(parent);
        child.setSize(200,200);
        child.open( );        
    }
}

The ChildShell constructor enables you to pass in a reference to the shell that will serve as the parent: ...

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.