简体   繁体   中英

JFace wizard passing variables

I'm a Java novice but am trying to produce a wizard using the Wizard class (org.eclipse.jface.wizard.Wizard)

Basically where I extend the Wizard in the constructor I addPage the two pages I want.

On my first page I take some credentials.

On the second page I want to run a query against the database using the credentials from the first page to populate a table with names.

How do I go about passing these values from the first to the second page?

To all intents and purposes my code at present is the same as http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/Asurveyusingawizard.htm except I obtain some strings from some text boxes on the first page and have a table on the second page.

I have read about containers and see there is a setData() method, is this something I can utilize?

Kind regards in advance

David

I like to create my data object in the Wizard and pass it into the constructor of each of my WizardPages. For example:

public void addPages() {
  data = new MyData()
  addPage(new FirstPage(data));
  addPage(new SecondPage(data));
  ...
}

One advantage to this approach is you have access to your data object during your wizard's performFinish.

Here's another way to do this:

PageOne pageOne = (PageOne) getWizard().getPreviousPage(this);

Supposing you are on PageTwo, and in PageOne you have defined your getters for the values you wish to use on PageTwo.

Another approach is to use a data class with static variables. For example, if you have a NewVehicleWizard, you may have to instantiate Car, Truck, or SUV (all subclasses of Vehicle). But that will not be known when the wizard is instantiated; that decision will be made in a VehicleTypePage, which could make the following method call when the option Truck is selected:

MyWizardData.setVehicle(new Truck());

MyWizardData will have a private static vehicle variable with static getter and setter. If the vehicle object is needed by a subsequent page or by the NewVehicleWizard itself, you can simply use the static getter:

Truck truck = (Truck)MyWizardData.getVehicle();
// ...work with truck here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM