corner imagecorner image
IDEPlatformPluginsDocs & SupportCommunityPartners

Application States In JavaFX Composer

Written by David Kaspar, adapted and maintained by Jeffrey Rubinoff

A JavaFX application's workflow can make use of states. A state represents a set of component property values that apply at the same point of time. Each state represents a snapshot of the application. The application is in one state when the application starts. The application switches to other states after the user clicks a button, chooses a radio button, or performs a similar action. A component's visibility, animations, and other properties can differ in different states. (Note that the component exists in all states.)

The States window lets you define states and switch between them. The Design view and the Properties and Navigator windows change their displays as you move between states.

In this tutorial, you design a simple UI with a label and a button. Press the button, and the application's state changes. The text in the label differs in different states.

Contents

Content on this page applies to NetBeans IDE 6.9

To complete this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE JavaFX download option
Java Development Kit (JDK) JDK 6 Update 13 or later

Designing the UI

For the purposes of this tutorial, you need a JavaFX Desktop application with some UI components. In this section you design the application.

To design the application:

  1. Choose File > New Project. The New Project wizard opens. Under Categories, select JavaFX. Under Projects, select JavaFX Desktop Business Application and click Next.
  2. The Name and Location page opens. Name the project States. Select a location for the project. Accept the default options and click Finish.
    Name and Location page for a new JavaFX desktop business application, showing name and default values
  3. The project is created and an empty design file opens in the Editor. Look for a Palette window and a Properties window on the right. If either window is missing, open it by going to Window > Palette (Ctrl+Shift+8) or Window > JavaFX Composer Properties, respectively.
  4. In the Palette window, expand the JavaFX Controls section and drag the following components into the Design:

    • Label (label)
    • Button (button)
  5. Select the label. Go to the Properties window and change the label's Text property value to "Master State".
  6. Select the button. Go to the Properties window and change its Text property value to "Next State".
  7. Still in the Properties for the button, click the Generate button next to the Action property.
    Properties window for the button component, showing the Generate Action button
  8. A list of action generation options opens. Choose "Generate: Go to next state". This generates a button action for going to the next state. The editor changes to the Source view, showing the buttonAction function that you generated. Change currentState.next() to currentState.nextWrapped(). Your design is now complete.
    Code snippet showing modified currentState call in code

Master State and Created States

All JavaFX applications have a default "master" state. You can see this in the States window of your application's Design view.

States window showing the default master state

Everything you design in an application's master state is created and set up when you start the application. You can work with components and set their properties as usual.

The value of a property is either its default value or is a value associated with a state. If a property is initiated after its creation or changed by a user in the master state, then the property's value is associated with the master state. The state associated with a property's value is indicated by the appearance of the button for opening its Details dialog. Default values are indicated by blank Detail dialog buttons, while master state values have the label '101'.

It is best practice to minimize the load of property changes that occur during a state change. Therefore edit properties in the master state as much as possible. Only the values that are changed in a particular state should be edited in a non-master state.

It is best practice to set one of the non-master states as the start state and never display the master state itself.

Properties window with Details buttons that indicate Default and Master State values

Creating a state

You can create multiple states for your design. You can create a state based on the master state or you can create a state as a duplicate of a non-master state. When you create a state, the new state inherits all its property values from its parent state. A state based on the master state inherits all property values from the master state. A state that is a duplicate of a non-master state inherits all the property values of the original non-master state. (The master state is the ultimate ancestor of all states. If a property value is not set in an ancestral non-master state, that property value is inherited from the master state.) After you create a state, you can edit that state's property values.

In the rest of this section, you create a state based on the master state and edit that state's property values.

To create a state based on the master state:

  1. In the States window in the Design view, click the Add State Add State button button.
    States window showing Add State button
  2. The Add State dialog opens. In this dialog, you can set the following properties:

    • The name of the new state.
    • Whether to inherit components and property values from the master state or duplicate another existing state.
    • Whether this new state is the starting state. In other words, whether this is the state of the application when it launches.

    Name the new state "Red" and accept all other default settings. Your application will now start in the Red state.

    Add State dialog
  3. Click OK. The Red state is created and selected in the States window.
  4. Select the label and change its Text property to "Red state".
  5. Click the Add button Add State button for the Text Fill property and select Color from the menu that appears.
    Dropdown menu that appears when the Add button is pressed for the Text Fill property, with the selection Color highlighted
  6. The Choose Color dialog opens. Select a shade of red that you like and click OK.
    The Choose Color dialog

You have now completed the Red state. The text of the label appears red in the Design view. The field for the Text Fill property of the label now says "color" and the Open Details button is enabled. The icon for the Open Details button has a small pencil to show that the values for this property are specific for the currently chosen state. The toolbar of the Properties window contains the identifier of the component (label) and the name of the currently chosen state (red).

Properties window for the label component in the red state

Duplicating a created state

You can create additional states by duplicating an existing created state. In this section, you duplicate the Red state to create the Blue state.

To duplicate a state:

  1. In the States window, click the Add State button.
  2. Name the state "Blue" and select the "Duplicate of" radio button. Select "Red" as the state to duplicate. Click OK.
    New state dialog where you duplicate the Red state to create the Blue state
  3. Select the label component. Look at the Properties window. Note that the label has inherited all the property settings from the Red state. Change the label's Text property to "Blue state".
  4. Click the Add button Add State button for the Text Fill property and select Color from the menu that appears.
  5. The Choose Color dialog appears. Select a shade of blue that you like and click OK. In the Design view, the text in the label changes to this color. The field for the Text Fill property of the label now says "color2".

You now have another state, Blue, that started as a duplicate of the Red state. In the States window, switch between Blue, Red, and master states. Note the change in appearance of the components in the Design view.

Transitioning Between States

You can set effects in your application that affect the transition of the application between states. In this tutorial, you add an animation to the application. The animation runs when the application changes state.

To add an animation that runs when the application changes state:

  1. Select the label component.
  2. In the Properties window, click the Transform button. The Transform properties display.
    Properties window for the label component in the Blue state, showing the Transform button and the Transform properties
  3. In the Transform properties, click the Open Details button for the Scale X property.
    Transform properties showing Open Details button for the Scale X property
  4. The Scale X property Details dialog opens. In this dialog, you set animations and values for the property in one or more states. Select the Blue state and type 1.5 in the specified value field. Note that the currentState box shows you that the value remains 1.0 in the master and Red states.
    Details dialog for Scale X property showing Blue state chosen and value set to 1.5

    Note: Instead of using a specified value, you can set the value using a custom code or generated function call. Select "Use value resolved from function:" and enter the custom code or the function call. Click the Generate button to create the specified function in the source code.

    Tip: If you set a property's values in a non-master state and realize that these should be the master values, click Set As Master. This button pushes the values as the new master values. On the other hand, if you want to discard your edited non-master state values, click the Reset As Master button. This button replaces all values in the selected states with the master values.

  5. Click the Select All button. This button selects all non-master states. You see that both Red and Blue states are selected. In the Animate drop-down box, select Linear. In the ms field, type 500.
    Details dialog for Scale X property showing linear animation running 500ms for Red and Blue states
  6. Click Close. Your application is complete.

In the States window, switch between Red, Blue, and master states. You see the appearance of the components in different states, but not the animation. The animation will run in the running application when a user clicks the Next State button.

Running the Project

 

Run the States project. (F6 or Run Main Project icon Run Main Project icon, if it is the main project.) The design file is saved and the project is compiled and started. Click the Next State button and watch the size of the label text change through a 500ms animation.

More Exercises

Here are some more exercises you can run to explore the use of states:



See Also

For more information about using NetBeans IDE to develop JavaFX applications, see the following resources: