JLWizard Documentation

Getting started

Let's create a simple wizard that will be used to add some data about a person to a database.

Suppose we need the following data: first name, last name, whether or not the person is an employee, department, salary and comments, of which only the first and last name are required (I know, it's extremely simplistic, but that way we can focus on the wizard component).

The source code of this demo wizard can be found in the CVS sources under the package com.ozziefix.jlwizard.demo.

Our wizard will have 4 pages:

  1. a General page, containing first and last name and the isEmployee check box.
  2. an Employee page, containing the department and salary.
  3. a comments page, containing the comments.
  4. an overview page, which displays all data read-only

The first thing we have to create is a model class, named DemoWizardModel, which implements the WizardModel interface, and has the six properties we need.

Next, we create the 4 pages of the wizard, by extending the AbstractWizardPage and adding some input fields to them. In these 4 classes, you should take note of the methods load(WizardModel model), store(WizardModel model) and validateInput().

load(WizardModel model)
Loads the data from the WizardModel in the page. This method is called right before the page is made visible.
store(WizardModel model)
Store the input from the page in the WizardModel. This method is called right before the page is made invisible, and after validateInput()
Use this method to validate the input from the page. This method is called before the input is stored in the WizardModel.

OK, now that we have the components, we can start building our wizard:

    wizard = WizardFactory.getWizardForCurrentLookAndFeel(parent);
    model = new DemoWizardModel();

The first line instantiates a wizard for the current look and feel, so if you're on a Windows platform, this will create a Windows-style wizard. The second line just creates an instance of our WizardModel.

Next, we create our wizard pages and add them to the wizard:

    generalPage = new DemoGeneralPage();
        "Enter first and last name of the person, and specify whether or not he/she is an employee"
    wizard.addPage("general", generalPage);

    employeePage = new DemoEmployeeDetailsPage();
    employeePage.setTitle("Employee details");
       "Enter the department and the salary of the new employee"
    wizard.addPage("employee", employeePage);

    commentsPage = new DemoCommentsPage();
    commentsPage.setDescription("Enter any comments here");
    wizard.addPage("comments", commentsPage);

    overviewPage = new DemoOverviewPage();
    overviewPage.setDescription("Check your input and then click finish");
    wizard.addPage("overview", overviewPage);

As you can see, adding pages to a wizard requires you to pass a String which identifies each page. This is needed for the navigation functionality of JLWizard. Each button on a Wizard button bar invokes the perform()-method of a WizardAction. This method returns a reference to another wizardpage, which is then shown in the wizard dialog. This makes it possible to change the order of the pages, based on some logic that is executed in the perform()-method of the WizardAction. If you've ever used Struts, this will sound very familiar to you.

Now let's wire up the pages using ForwardActions, a convenience WizardAction that just forwards to another page.

    generalPage.setNextAction(new ForwardAction("employee"));

    employeePage.setNextAction(new ForwardAction("comments"));
    employeePage.setPreviousAction(new ForwardAction("general"));

    commentsPage.setNextAction(new ForwardAction("overview"));
    commentsPage.setPreviousAction(new ForwardAction("employee", "general"));
    commentsPage.setFinishAction(new DemoFinishAction());

    overviewPage.setPreviousAction(new ForwardAction("comments"));
    overviewPage.setFinishAction(new DemoFinishAction());

    wizard.setStartAction(new ForwardAction("general"));

Note the last line: the start action of a wizard determines which page will be shown first.

That's it! We now have a simple wizard. All we need to do now is show it:

    wizard.setSize(500, 400);

This should get you an idea how to create wizards using JLWizard. In the next version of this document, I shall give an example of branching.

API Javadocs

You can browse the API Javadocs here.

Home Download Docs Links