Copyright (c) 2003-2004 Jurgen Lust
Last updated: $Date: 2005/01/25 01:42:25 $
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:
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().
OK, now that we have the components, we can start building our wizard:
wizard = WizardFactory.getWizardForCurrentLookAndFeel(parent); model = new DemoWizardModel(); wizard.setModel(model);
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(); generalPage.setTitle("General"); generalPage.setDescription( "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"); employeePage.setDescription( "Enter the department and the salary of the new employee" ); wizard.addPage("employee", employeePage); commentsPage = new DemoCommentsPage(); commentsPage.setTitle("Comments"); commentsPage.setDescription("Enter any comments here"); wizard.addPage("comments", commentsPage); overviewPage = new DemoOverviewPage(); overviewPage.setTitle("Overview"); 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); wizard.start();
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.
You can browse the API Javadocs here.