Demonstrates Wizard

In SWT, the class WizardDialog shows a wizard to the user. In the given example, we have add four pages to the Wizard i.e. NamePage, QuestionPage,

Demonstrates Wizard

Demonstrates Wizard

     

This section illustrates you how to implement the wizard.

In SWT, the class WizardDialog shows a wizard to the user. In the given example, we have add four pages to the Wizard i.e. NamePage, QuestionPage, MoreInformationPage and ThanksPage by using the method addPage(). The method performFinish() defined by the class Wizard  implement finish processing for the wizard. The class WizardPage implements the wizard pages. In the NamePage, we have create a form including two fields name and email. If the user enter the fields ,he can move to the next page, i.e QuestionPage. In this page, we have asked a question whether he want to ask any question. For selection, we have create two radio buttons Yes or No. If the user clicks yes button, then only he will move to the MoreInformationPage, otherwise he will move to the Thanks page. In the MoreInformationPage, we have create a text box to ask the question.

The method setDefaultPageImageDescriptor() of class Wizard sets the image descriptor for the wizard. The class ImageDescriptor allows to create a SWT image. The method createFromFile() creates and returns a new image descriptor from a file.

 The method setPageComplete() of Boolean type sets whether this page is complete or not. The method getNextPage() of class IWizardPage returns the wizard page that would have to be shown if the user press the Next button.

Here is the code of WizardExample.java

import java.util.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.jface.wizard.*;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.jface.resource.ImageDescriptor;

public class WizardExample {
  public void run() {
  Display display = new Display();
  Shell shell = new Shell(display);
  WizardDialog dlg = new WizardDialog(shell, new Wizard1());
  dlg.open();
  display.dispose();
  }
  public static void main(String[] args) {
  new WizardExample().run();
  }
}
class Wizard1 extends Wizard {
  public Wizard1() {
  addPage(new NamePage());
  addPage(new QuestionPage());
  addPage(new MoreInformationPage());
  addPage(new ThanksPage());
  setDefaultPageImageDescriptor(ImageDescriptor.createFromFile(null,
   "logo.gif"));
  }
  public boolean performFinish() {
  return true;
  }
}
class NamePage extends WizardPage {
  Text first,last;
  String name = "";
  String email = "";
  public NamePage() {
 super("Entry Form");
  }
  public void createControl(Composite parent) {
  Composite composite = new Composite(parent, SWT.NONE);
  composite.setLayout(new GridLayout(2,false));

  new Label(composite, SWT.LEFT).setText("Name:");
  final Text first = new Text(composite, SWT.BORDER);
  first.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

  new Label(composite, SWT.LEFT).setText("Email Id:");
  final Text last = new Text(composite, SWT.BORDER);
  last.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

  first.addModifyListener(new ModifyListener() {
  public void modifyText(ModifyEvent event) {
  name = first.getText();
  setPageComplete(name.length() > 0 && email.length() > 0);
 }
  });
  last.addModifyListener(new ModifyListener() {
  public void modifyText(ModifyEvent event) {
  email = last.getText();
  setPageComplete(name.length() > 0 && email.length() > 0);
  }
  });
  setControl(composite);
  }
  public String getName() {
  return name;
  }
  public String getEmail() {
  return email;
  }
}
class QuestionPage extends WizardPage {
  private Button yes;
  private Button no;
  public QuestionPage() {
  super("Complaints");
  }
  public void createControl(Composite parent) {
  Composite composite = new Composite(parent, SWT.NONE);
  composite.setLayout(new GridLayout(2, true));

  new Label(composite, SWT.LEFT).setText("Do you have any Questions?");
  Composite yesNo = new Composite(composite, SWT.NONE);
  yesNo.setLayout(new FillLayout(SWT.VERTICAL));

  yes = new Button(yesNo, SWT.RADIO);
  yes.setText("Yes");

  no = new Button(yesNo, SWT.RADIO);
  no.setText("No");

  setControl(composite);
  }
 public IWizardPage getNextPage() {
  if (yes.getSelection()) { return super.getNextPage(); }
  return getWizard().getPage("Thanks");
  }
}
class MoreInformationPage extends WizardPage {
  public MoreInformationPage() {
  super("More Info");
  }
  public void createControl(Composite parent) {
  Composite composite = new Composite(parent, SWT.NONE);
  composite.setLayout(new GridLayout(1, false));

  new Label(composite, SWT.LEFT).setText("Please Ask your question");
  Text text = new Text(composite,SWT.MULTI|SWT.BORDER|SWT.V_SCROLL);
  text.setLayoutData(new GridData(GridData.FILL_BOTH));
  
  setControl(composite);
  }
}
class ThanksPage extends WizardPage {
  public ThanksPage() {
  super("Thanks");
  }
  public void createControl(Composite parent) {
  Label label = new Label(parent, SWT.CENTER);
  label.setText("Thanks!");
  setControl(label);
  }
}

Output will be displayed as:

Download Source Code