Working with Dialogs

As our final SWT example, we'll take a look at creating new SWT dialogs and recovering data that the user entered in them. SWT comes with a number of prebuilt dialog classes, such as the FileDialog and DirectoryDialog classes, but in this example we're going to create our own custom dialog. We'll display a custom dialog with the message "OK to proceed?" along with OK and Cancel buttons and determine which button the user clicked. In the main app shell, we'll display a button named opener that the user can click to display the dialog, and a text control to display the selection the user made:

final Button opener = new Button(shell, SWT.PUSH);
opener.setText("Click Me");
opener.setBounds(20, 20, 50, 25);
final Text text = new Text(shell, SWT.SHADOW_IN);
text.setBounds(80, 20, 100, 25);


The dialog will also be a Shell object. Here are the styles you can use:


SWT.APP_MODAL

Makes a dialog box app modal


SWT.BORDER

Adds a border


SWT.H_SCROLL

Adds a horizontal scrollbar


SWT.V_SCROLL

Adds a vertical scrollbar


SWT.CLOSE

Adds a close button


SWT.DIALOG_TRIM

Styles a dialog box


SWT.MIN

Adds a minimize button


SWT.MAX

Adds a maximize button


SWT.NO_TRIM

Ensures no trimmings are used


SWT.RESIZE

Handles moving and resizing


SWT.TITLE

Allows space for a title

To create our dialog, we'll use the styles SWT.APP_MODAL | SWT.DIALOG_TRIM when creating it. Making it app modal means that the user must dismiss the dialog before working with any other window in the app:

final Shell dialog = new Shell(shell, SWT.APP_MODAL |
 SWT.DIALOG_TRIM);
dialog.setText("Dialog");
dialog.setSize(150, 100);


In this example, the dialog is designed to display a prompt to the user ("OK to proceed?") in a label and two buttons, OK and Cancel. We'll add these with this code:

final Label label = new Label(dialog, SWT.NONE);
label.setText("OK to proceed?");
label.setBounds(35, 5, 100, 20);
final Button okButton = new Button(dialog, SWT.PUSH);
okButton.setBounds(20, 35, 40, 25);
okButton.setText("OK");
Button cancelButton = new Button(dialog, SWT.PUSH);
cancelButton.setBounds(70, 35, 40, 25);
cancelButton.setText("Cancel");


To handle the OK and Cancel buttons, we'll use a Listener object, creating that object with an anonymous inner class, as usual. The difficulty here is in letting the code in that inner class indicate which selection the user made, because it's illegal for inner class code to set the value of a variable in the enclosing class. However, you can let code in an inner class set data in an array in the enclosing class, which is what we'll do here. Here's how we determine which button was clicked and set the Boolean response[0] accordingly in the app's main code—note that we also close the dialog here because the user clicked either the OK or Cancel button:

final boolean [] response = new boolean[1];
response[0] = true;
Listener listener = new Listener( ) {
 public void handleEvent(Event event) {
 if(event.widget == okButton){
 response[0] = true;
 }else{
 response[0] = false;
 }
 dialog.close( );
 }
};


Now you can assign this listener to the OK and Cancel buttons. We also need to assign a listener to the opener button to open and display the dialog in the first place:

okButton.addListener(SWT.Selection, listener);
cancelButton.addListener(SWT.Selection, listener);
Listener openerListener = new Listener( ) {
 public void handleEvent(Event event) {
 dialog.open( );
 }
};
opener.addListener(SWT.Selection, openerListener);


We also need a message loop for the dialog, which we'll add as well. Finally, when the dialog is closed, we'll check the return value in response[0] and display a message to indicate which button the user clicked:

while(!dialog.isDisposed( )) {
 if(!display.readAndDispatch( )) display.sleep( );
}
if(response[0]){
 text.setText("You clicked OK");
} else {
 text.setText("You clicked Cancel");
}


That completes the code; you can see the full listing in Example 8-5.

Example 8-5. Creating SWT dialogs
package org.eclipse.ch08;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class Ch08_05 {
 public static void main(String [] args) {
 Display display = new Display( );
 Shell shell = new Shell(display);
 shell.setSize(200, 200);
 shell.setText("Dialogs");
 shell.open( );
 final Button opener = new Button(shell, SWT.PUSH);
 opener.setText("Click Me");
 opener.setBounds(20, 20, 50, 25);
 final Text text = new Text(shell, SWT.SHADOW_IN);
 text.setBounds(80, 20, 100, 25);
 final Shell dialog = new Shell(shell, SWT.APP_MODAL |
 SWT.DIALOG_TRIM);
 dialog.setText("Dialog");
 dialog.setSize(150, 100);
 final Label label = new Label(dialog, SWT.NONE);
 label.setText("OK to proceed?");
 label.setBounds(35, 5, 100, 20);
 final Button okButton = new Button(dialog, SWT.PUSH);
 okButton.setBounds(20, 35, 40, 25);
 okButton.setText("OK");
 Button cancelButton = new Button(dialog, SWT.PUSH);
 cancelButton.setBounds(70, 35, 40, 25);
 cancelButton.setText("Cancel");
 final boolean [] response = new boolean[1];
 response[0] = true;
 Listener listener = new Listener( ) {
 public void handleEvent(Event event) {
 if(event.widget == okButton){
 response[0] = true;
 }else{
 response[0] = false;
 }
 dialog.close( );
 }
 };
 okButton.addListener(SWT.Selection, listener);
 cancelButton.addListener(SWT.Selection, listener);
 Listener openerListener = new Listener( ) {
 public void handleEvent(Event event) {
 dialog.open( );
 }
 };
 opener.addListener(SWT.Selection, openerListener);
 while(!dialog.isDisposed( )) {
 if(!display.readAndDispatch( )) display.sleep( );
 }
 if(response[0]){
 text.setText("You clicked OK");
 } else {
 text.setText("You clicked Cancel");
 }
 while(!shell.isDisposed( )) {
 if(!display.readAndDispatch( )) display.sleep( );
 }
 display.dispose( );
 }
}


Running this example displays the shell you see in Screenshot-9, with the button the user can click to display the dialog and the text control to display the results.

Screenshot-9. The Ch08_05 app
Java figs/ecps_0809.gif

When you click the button, the dialog appears, as shown in Screenshot-10. Because we're creating a dialog here, there's no maximize or minimize buttons in the dialog, and its border is not resizeable. The OK button is highlighted, making it the default button; if the user closes the dialog by pressing Enter, the OK button is selected by default.

Screenshot-10. Displaying a dialog
Java figs/ecps_0810.gif

Clicking one of the buttons in the dialog closes the dialog and displays the button you've clicked in the text control in the main window, as you see in Screenshot-11. In this way, we've been able to communicate between windows, catching the user's response and displaying it.

Screenshot-11. Recovering the result from a dialog
Java figs/ecps_0811.gif
      
Comments