Creating a new project Wizard

A wizard is a step-by-step assistant that helps the user to enter information in a structured manner.

NetBeans offers the developer the option to create a wide variety of wizards. These wizards can be non-sequential in nature, further enhancing the user experience.

Getting ready

To create a NetBeans Module follow the steps below:

  1. Create a new project, this can be achieved by either clicking File and then New Project or pressing Ctrl+Shift+N.
  2. On New Project window on Categories side select NetBeans Modules and on Projects side select Module and click Next >.
  3. Under Name and Location, Under Project Name enter NBCookbookWizard and leave all the default values as they are and click Next >.
  4. Under Basic Module Configuration, Under Code Name Base enter com.packtpub.
  5. Then select Generate XML Layer, leave the default value and click Finish.
Getting ready

How to do it...

Now with the project created, perform the following steps to create the wizard:

  1. Right-click project node, select New then Other.
  2. On New File window on Categories side choose Module Development and on the File Types side select Wizard and click Next >.
  3. On New Wizard dialog, Wizard Type: Leave Registration type with its own default value (Custom).
  4. Leave with the default value Wizard Type Sequence (Static).
  5. Under Number of Wizard Panels enter 3 and click Next.
    How to do it...
  6. Under Name and Location: Under enter first under Class Name Prefix and let NetBeans fill the other details. Then click Finish.
How to do it...

Now let's modify FirstWizardAction.java, replacing the following line:

wizardDescriptor.setTitle("Your wizard dialog title here");

with this:

wizardDescriptor.setTitle("Project Wizard");

Then save the file.

Now to add components to our panel. Open FirstVisualPanel1.java and add the following Swing components:

  • 2 Labels
  • 2 TextFields
  1. Replace the default text of the first Label (can be done by double-clicking on it) and write Name.
  2. Replace the text of the second Label with Location.
  3. Then right-click on the other two TextFields, select Edit Text in each one of them and delete the attached text.
  4. Now click on the Source button and on the getName() method replace the String Step #1 with Name and Location.
  5. Save the file.

Open FirstVisualPane2.java and add the following Swing components:

  • 2 Labels
  • 2 RadioButtons
  • 1 TextArea

Replace the default text of the first Label with Description, and the text of the second Label with Type.

Click on the first RadioButton and on the Properties window (on the right side of the IDE) under the Other Properties sub-section replace the text in the Label property with Web.

How to do it...

Do the same thing to the second RadioButton, but this time under Label enter Desktop.

Now click on the Source button and on the getName() method replace the String Step #2 with Project Description, then the file. And finally the last FirstVisualPane. Open FirstVisualPane3.java and add the following Swing components:

  • 2 Labels
  • 1 ComboBox
  • 1 TextArea
  1. Replace the default text of the first Label with Server.
  2. Replace the text of the second Label with Environment.
  3. For the ComboBox click on the Properties window, then under the Properties sub-section click on the find the model property.

On the right-hand side of the property there is a button with three dots (…) that will let the developer replace the text with the following:

  • Oracle GlassFish Server
  • Apache Tomcat
  • JBoss
How to do it...

Now click on the Source button and on the getName() method replace the String Step #3 with Servers. Click OK and save the file.

Open layer.xml and replace the following code:

<filesystem/>

with

<filesystem>
<folder name="Toolbars">
<folder name="File">
<file name="com-packtpub-FirstWizardAction.shadow">
<attr name="originalFile" stringvalue="Actions/File/com-packtpub-FirstWizardAction.instance"/>
</file>
</folder>
</folder>
<folder name="Actions">
<folder name="File">
<file name="com-packtpub-FirstWizardAction.instance">
<attr name="delegate" newvalue="com.packtpub.FirstWizardAction"/>
<attr name="instanceCreate" methodvalue="org.openide.awt.Actions.alwaysEnabled"/>
</file>
</folder>
</folder>
</filesystem>

Save the file and click on Run.

How it works...

When Generate XML Layer is selected NetBeans creates the layer.xml file in the specified directory (which can be changed depending on preference).

The layer.xml file registers our component to NetBeans, in our case a new wizard will be added to the toolbar.

On the New Wizard dialog on the Wizard type step we have selected Registration Type as Custom so that the user will have a sequential progress in each wizard step.

Selecting Custom as Wizard Type also has an effect on which files NetBeans will create for arranging the wizard.

The files created by NetBeans for the wizard are:

  • FirstWizardAction.java: responsible for registration and behavior of the component as a whole.
  • FirstVisualPanelX.java: one created for each of the panels. X can be replaced by the number of selected panels. It is where the wizard is designed and other components, like labels and textfields are placed.
  • FirstWizardPanelX.java: as with FirstVisualPanel.java, one is created for each panel. This is where data that was entered in the Visual Panels is then retrieved and stored so that it can be available to the wizard action.

This is also an easier and faster way to create wizards other than fiddling around with combinations of Registration Types and Wizard Step Sequence.

For the Wizard Step Sequence option we went for keeping things simple and selecting the Static option. This prevents the wizard user this will prevent it from "jumping" between wizard panels giving a more linear progress which might give a more restrictive experience. For the developer it means easier way to implement it, since the developer wont need to keep track of all possible combinations.

After saving and running the files the results should look like this: FirstVisualPanel1.java.

How it works...

There's more...

Implementing another kind of wizard. Also, how to add your own icon to the toolbar.

Different wizard behavior

There are other ways the wizard can be created that will result in different interactions with the user and vary the amount of work/flexibility involved.

On the second step of the New Wizard dialog, the Wizard type, choose Registration Type as Custom and Wizard Step Sequence as Dynamic.

This new wizard will provide the functionality of jumping between panels. This will provide more flexibility to the user but with the added expense to the developer that will be forced to handle the non-linear progression between panels.

The file list is almost the same as the one we have seen in the recipe example. The difference being that now we will have the WizardIterator.java, which is created to handle these panel changes.

The file list is also almost identical to the previous examples but with the notable addition of an HTML file which is responsible for holding the description of the wizard itself.

As with the previous example, the WizardIterator is responsible for providing the directions that the wizard will take.

Adding an icon

Notice that when running our example there is no default icon for the placeholder of our new wizard. NetBeans uses a missing icon image as the default one.

To add an icon follow the steps below:

  1. Open layer.xml.
  2. Find the following line:
    <file name="com-packtpub-FirstWizardAction.instance">
    
  3. Right before the</file> tag add (Supposing our image is named as picon.png):
    <attr name="iconBase" stringvalue="com/packtpub/picon.png"/>
    
  4. Now add the image to the directory where all the Java sources are placed, in our case the src/com/packtpub.
  5. Save and re-run the example. The Wizard with the new image is placed near the File menu toolbar.
Adding an icon
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.120.200