Arikado
Arikado

Reputation: 65

Initialize Spinner control in fxml

New JDK is here:

JDK 8u40 release includes new JavaFX UI controls; a spinner control, formatted-text support, and a standard set of alert dialogs.

I want to initiliaze the Spinner with an IntegerSpinnerValueFactory in fxml. I tried like the following:

<Spinner><valueFactory><SpinnerValueFactory ???????? /></valueFactory></Spinner>

There is little documentation for the new control and considering that is only java in class coding.

Any idea of how to initialize it?

Upvotes: 5

Views: 4806

Answers (1)

Jos&#233; Pereda
Jos&#233; Pereda

Reputation: 45486

If you have a look at the Spinner class, you have several constructors available.

For instance:

public Spinner(@NamedArg("min") int min,
               @NamedArg("max") int max,
               @NamedArg("initialValue") int initialValue) {
    this((SpinnerValueFactory<T>)new SpinnerValueFactory.IntegerSpinnerValueFactory(min, max, initialValue));
}

According to this answer:

The @NamedArg annotation allows an FXMLLoader to instantiate a class that does not have a zero-argument constructor.

so you can use min, max and initialValue as parameters for the Spinner on your FXML file:

<Spinner fx:id="spinner" min="0" max="100" initialValue="3" >
      <editable>true</editable>
</Spinner>

Note that your IDE may complain about it with warnings about Class javafx.scene.control.Spinner doesn't support property 'min'... But you can build and run the project.

Upvotes: 9

Related Questions