5

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?

Arikado
  • 65
  • 1
  • 6

1 Answers1

9

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.

Community
  • 1
  • 1
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • Used it on my fxml like `` But it's not truly editable. It just returns to previous value before typing the desired value and using the increment/decrement arrows. I will just keep using my custom control until I fully figure out how to achieve my desired behaviour with Spinner. Thanks – Arikado Mar 05 '15 at 16:24
  • Sure, you can set `editable` inline, that's even better. – José Pereda Mar 05 '15 at 16:29