SWT Dialog Class : Dialog « SWT JFace Eclipse « Java






SWT Dialog Class

SWT Dialog Class

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class DialogClass {
  static boolean deleteFlag = false;

  public static void main(String[] args) {
    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);

    final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL
        | SWT.DIALOG_TRIM);
    dialog.setText("Delete File");
    dialog.setSize(250, 150);

    final Button buttonOK = new Button(dialog, SWT.PUSH);
    buttonOK.setText("OK");
    buttonOK.setBounds(20, 55, 80, 25);

    Button buttonCancel = new Button(dialog, SWT.PUSH);
    buttonCancel.setText("Cancel");
    buttonCancel.setBounds(120, 55, 80, 25);

    final Label label = new Label(dialog, SWT.NONE);
    label.setText("Delete the file?");
    label.setBounds(20, 15, 100, 20);

    Listener listener = new Listener() {
      public void handleEvent(Event event) {
        if (event.widget == buttonOK) {
          deleteFlag = true;
        } else {
          deleteFlag = false;
        }
        dialog.close();
      }
    };

    buttonOK.addListener(SWT.Selection, listener);
    buttonCancel.addListener(SWT.Selection, listener);

    Listener buttonListener = new Listener() {
      public void handleEvent(Event event) {
        dialog.open();
      }
    };

    button.addListener(SWT.Selection, buttonListener);

    while (!dialog.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }

    if (deleteFlag) {
      text.setText("File deleted.");
    } else {
      text.setText("File not deleted.");
    }

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}


           
       








Related examples in the same category

1.TitleAreaDialog: MailDialog
2.Dialog ShellDialog Shell
3.MessageBox ExampleMessageBox Example
4.Number Input DialogNumber Input Dialog
5.Dialog ExamplesDialog Examples
6.Demonstrates FileDialogDemonstrates FileDialog
7.A facade for the save FileDialog
8.How to create your own dialog classesHow to create your own dialog classes
9.Demonstrates the FontDialog classDemonstrates the FontDialog class
10.Demonstrates the ColorDialog classDemonstrates the ColorDialog class
11.Demonstrates the DirectoryDialog classDemonstrates the DirectoryDialog class
12.Demonstrates the custom InputDialog classDemonstrates the custom InputDialog class
13.Demonstrates the MessageBox classDemonstrates the MessageBox class
14.Dialog Example
15.Shell Dialog ExampleShell Dialog Example
16.Color Dialog ExampleColor Dialog Example
17.File Dialog ExampleFile Dialog Example
18.Font Dialog ExampleFont Dialog Example
19.Yes No Icon MessageBoxYes No Icon MessageBox
20.Print Dialog ExamplePrint Dialog Example
21.Prevent escape from closing a SWT dialogPrevent escape from closing a SWT dialog
22.Create a dialog shell (prompt for a value)Create a dialog shell (prompt for a value)
23.Create a SWT dialog shellCreate a SWT dialog shell