SWT Study Notes section 1 Shell

Source: Internet
Author: User

1. Display and Shell

We use two SWT classes to create windows:DisplayAndShell.DisplayIs the class responsible for managing the interaction between all SWT Widgets

And the underlying operating system. It is inDisplayThat you find methods that enable you to directly query the operating system for information about

Things such as which control currently has the focus and what windows are currently open and attached to the display. You will not need to interact directly

With the display very often.

The second class,Shell, Is much more important to the programmer. InstancesShellRepresent windows which are currently being managed by the Desktop

(On MS windows) or the Windows Manager (on Unix or Linux systems). shells can be created either directly on the display, or within the confines of a parent shell.

 

2. A very simple Shell

import org.eclipse.swt.widgets.*;public class SimpleShell {         SimpleShell( )    {        Display d = new Display( );        Shell s = new Shell(d);        s.setSize(500,500);        s.open( );        while(!s.isDisposed( )){            if(!d.readAndDispatch( ))                d.sleep( );        }        d.dispose( );    }}
 
// A runner class to execute examples
public class Runner {        public static void main(String[] args){        SimpleShell ss = new SimpleShell( );            }}
Explanation:
The final code segment constitutes the shell's event loop:
while(!s.isDisposed( )){    if(!d.readAndDispatch( ))      d.sleep( );    }

Shells respond to events, some fired by the operating system, others by user actions. Examples of events are things such as the user clicking the maximize

Button or closing the window created by the shell. The event loop continuously listens for these events and dispatches them to the appropriate Handler

(This is the purpose ofReadanddispatch ()Method ofDisplayClass). This means that every shell you create must have this event loop.

Failure to provide the event loop results in the shell being created, then immediately disposed of. The event loop continues to execute

TheIsdisposed ()Method ofShellClass returns true, indicating that the window has been closed by the user.

The last line releases the memory resources captured when you created the display:

d.dispose( );

It is important in SWT programming to remember to dispose of any widget you explicitly create. This prevents memory leaks.

To specify a style for a shell, the SWT provides us with a set of enumerated values encapsulated in another class calledSWT.SWTClass is located in

TheOrg. Eclipse. SWTPackage. For shells, the enumerated values areBorder,Close,Min,Max,No_trim,Resize, AndTitle. Also,

Two convenience valuesShell_trimAndDialog_trimCombine several of the style attributes to create two common looks for Windows.

Shell S = new shell (D, SWT. Close | SWT. Resize );

 

Shell_trim: Can maximize, minimize, can reset the size, can close

Dialog_trim: it cannot be maximized, minimized, reset, or disabled.

 

Display Effect:

 

3. example. Opening childshell within a parent Shell

import org.eclipse.swt.widgets.*;public class ChildShellExample {    Display d = new Display( );            ChildShellExample( )    {            Shell s = new Shell(d);        s.setSize(500,500);        s.open( );        ChildShell cs = new ChildShell(s);        while(!s.isDisposed( )){            if(!d.readAndDispatch( ))                d.sleep( );        }        d.dispose( );    }}

 

 

4. Dialog

TheDialogClass enables you to create custom dialosthose on which you can place any widget you desire. A dialog is simply another typeShell,

Could t that it extendsDialogClass, which encapsulates some additional methods and accepts additional style attributes.

TheDialogClass has two style attributes:SWT. application_modalAndSWT. system_modal.Application_modal, As the name implies,

Will cause the dialog to halt all processing in the application until the dialog is dismissed.System_modalWill prevent the user from login Ming other tasks

In any application running in the operating system while the dialog is open (although background tasks will still run on most platforms ).

AsShell, You specify the dialog type at construction time by passing the style attributes desired toDialogConstructor.

 

// Example an application Modal Dialog
import org.eclipse.swt.SWT;import org.eclipse.swt.widgets.*;public class DialogExample extends Dialog {        DialogExample(Shell parent)    {        super(parent);            }    public String open( )    {        Shell parent = getParent( );         Shell dialog = new Shell(parent,            SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);         dialog.setSize(100,100);        dialog.setText("A Dialog");         dialog.open( );         Display display = parent.getDisplay( );         while (!dialog.isDisposed( ))         { if (!display.readAndDispatch( )) display.sleep( );         }         return "After Dialog";    }}

 

// Example. Opening a dialog
import org.eclipse.swt.widgets.*;public class ShellDialogExample {    ShellDialogExample( )    {        Display d = new Display( );        Shell s = new Shell(d);        s.setSize(300,300);        s.open( );        DialogExample de = new DialogExample(s);        String result = de.open( );        System.out.println(result);        while(!s.isDisposed( )){            if(!d.readAndDispatch( ))                d.sleep( );        }        d.dispose( );    }}

 

Display Effect: only after the dialog box is completed can the original form be executed! However, childshell is not. It does not affect the running of parentshell.

 

4. Set the shell icon

Shell. setimage (new image (display, "C: \ java_coffee.ico "));

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.