Skip to Main Content
Eclipse Cookbook
book

Eclipse Cookbook

by Steve Holzner
June 2004
Beginner to intermediate content levelBeginner to intermediate
364 pages
7h 38m
English
O'Reilly Media, Inc.
Content preview from Eclipse Cookbook

9.2. Creating Dialogs

Problem

You need some input from the user, and a dialog box would be perfect.

Solution

Create a new shell, configure it as a dialog, display it, and recover the data the user entered.

Discussion

SWT has message boxes and a number of prebuilt dialogs that come with it, such as the FileDialog class that creates file dialogs or the FontDialog class that enables users to choose a font. But if you need more than those prebuilt dialogs, you can build your own custom dialog. In this example, we’re going to assume you want to confirm the user’s decision to delete a file. When the user clicks a button labeled Delete File, the application (DialogApp at this book’s site) displays a confirming dialog with OK and Cancel buttons. You can then determine which one the user clicked.

The application starts by creating a shell with the Delete File button and a text widget in whch we’ll display the response to the user’s button click:

Display display = new Display( );
Shell shell = new Shell(display);
shell.setText("Dialog Example");
shell.setSize(300, 200);
shell.open( );

final Button button = new Button(shell, SWT.PUSH);
button.setText("Delete File");
button.setBounds(20, 40, 80, 25);

final Text text = new Text(shell, SWT.SHADOW_IN);
text.setBounds(140, 40, 100, 25);
         .
         .
         .

To create a dialog, you create a shell and give it the style SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM, making it an application-modal dialog (the user must dismiss it before continuing with the application) and styling ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Eclipse

Eclipse

Steve Holzner
Official Eclipse 3.0 FAQs

Official Eclipse 3.0 FAQs

John Arthorne, Chris Laffra

Publisher Resources

Supplemental ContentCatalog PageErrata