Dialog Examples : Dialog « SWT JFace Eclipse « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » SWT JFace Eclipse » DialogScreenshots 
Dialog Examples
Dialog Examples


/*******************************************************************************
 * All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
 
 * Created on 2004-5-13 22:13:55 by JACK $Id$
 *  
 ******************************************************************************/



import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class DialogExamples extends ApplicationWindow {

  /**
   @param parentShell
   */
  public DialogExamples(Shell parentShell) {
    super(parentShell);
  }

  /*
   * (non-Javadoc)
   
   * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite)
   */
  protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NULL);

    composite.setLayout(new GridLayout());

    /* ------ MessageDialog ------------- */
    // openQuestion
    final Button buttonOpenMessage = new Button(composite, SWT.PUSH);
    buttonOpenMessage.setText("Demo: MessageDialog.openQuestion");
    buttonOpenMessage.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        
        boolean answer =
          MessageDialog.openQuestion(
            getShell(),
            "A Simple Question",
            "Is SWT/JFace your favorite Java UI framework?");
        System.out.println("Your answer is " (answer ? "YES" "NO"));
      }
    });

    final Button buttonMessageDialog = new Button(composite, SWT.PUSH);
    buttonMessageDialog.setText("Demo: new MessageDialog");
    buttonMessageDialog.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        MessageDialog dialog =
          new MessageDialog(
            getShell(),
            "Select your favorite Java UI framework",
            null,
            "Which one of the following is your favorite Java UI framework?",
            MessageDialog.QUESTION,
            new String[] { "AWT""Swing""SWT/JFace" },
            2);
        int answer = dialog.open();
        
        switch (answer) {
          case -1// if the user closes the dialog without clicking any button.
            System.out.println("No selection");
            break;
            
          case :
            System.out.println("Your selection is: AWT");
            break;
          case :
            System.out.println("Your selection is: Swing");
            break;
          case :
            System.out.println("Your selection is: SWT/JFace");
            break;
          
        }
      }
    });
    
    /* ------ InputDialog ------------- */
    final Button buttonInputDialog = new Button(composite, SWT.PUSH);
    buttonInputDialog.setText("Demo: InputDialog");
    buttonInputDialog.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        IInputValidator validator = new IInputValidator() {
          public String isValid(String newText) {
            if(newText.equalsIgnoreCase("SWT/JFace"||
                newText.equalsIgnoreCase("AWT"||
                newText.equalsIgnoreCase("Swing"))
              return null;
            else
              return "The allowed values are: SWT/JFace, AWT, Swing";
          }
        };
        InputDialog dialog = new InputDialog(getShell()"Question""What's your favorite Java UI framework?""SWT/JFace", validator);
        if(dialog.open() == Window.OK) {
          System.out.println("Your favorite Java UI framework is: " + dialog.getValue());
        }else{
          System.out.println("Action cancelled");
        }
      }
    });
    
    /* ------ ProgressMonitorDialog ------------- */
    final Button buttonProgressDialog = new Button(composite, SWT.PUSH);
    buttonProgressDialog.setText("Demo: ProgressMonitorDialog");
    buttonProgressDialog.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        IRunnableWithProgress runnableWithProgress = new IRunnableWithProgress() {
          public void run(IProgressMonitor monitor)
            throws InvocationTargetException, InterruptedException {
            monitor.beginTask("Number counting"10);
            for(int i=0; i<10; i++) {
              if(monitor.isCanceled()) {
                monitor.done();
                return;
              }
                
              System.out.println("Count number: " + i);
              monitor.worked(1);
              Thread.sleep(500)// 0.5s.
            }
            monitor.done();
          }
        };
        
        ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
        try {
          dialog.run(true, true, runnableWithProgress);
        catch (InvocationTargetException e) {
          e.printStackTrace();
        catch (InterruptedException e) {
          e.printStackTrace();
        }
        
        
      }
    });    
    return super.createContents(parent);
  }

  public static void main(String[] args) {
    ApplicationWindow window = new DialogExamples(null);
    window.setBlockOnOpen(true);
    window.open();
  }
}


           
       
Related examples in the same category
1.TitleAreaDialog: MailDialog
2.Dialog ShellDialog Shell
3.MessageBox ExampleMessageBox Example
4.Number Input DialogNumber Input Dialog
5.Demonstrates FileDialogDemonstrates FileDialog
6.A facade for the save FileDialog
7.How to create your own dialog classesHow to create your own dialog classes
8.Demonstrates the FontDialog classDemonstrates the FontDialog class
9.Demonstrates the ColorDialog classDemonstrates the ColorDialog class
10.Demonstrates the DirectoryDialog classDemonstrates the DirectoryDialog class
11.Demonstrates the custom InputDialog classDemonstrates the custom InputDialog class
12.Demonstrates the MessageBox classDemonstrates the MessageBox class
13.Dialog Example
14.Shell Dialog ExampleShell Dialog Example
15.Color Dialog ExampleColor Dialog Example
16.File Dialog ExampleFile Dialog Example
17.Font Dialog ExampleFont Dialog Example
18.Yes No Icon MessageBoxYes No Icon MessageBox
19.Print Dialog ExamplePrint Dialog Example
20.SWT Dialog ClassSWT Dialog Class
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.