0

If I create an instance of a Spinner using the constructor with arguments, that spinner will not show the given values. But when using a SpinnerValueFactory() and apply it to the spinner, it loads the initial value and sets min,max.

The documentation says:

Spinner(double min, double max, double initialValue, double amountToStepBy)

Creates a Spinner instance with the value factory set to be an instance of SpinnerValueFactory.DoubleSpinnerValueFactory.

So I would expect the constructor to set its own SpinnerValueFactory. My question is, why does that not work? How should one use those constructors?

In order to keep my code better readable and shorter, I would love to only use the constructor without an individual SpinnerValueFactory() for each Spinner (I need a couple of different ones).

What I would expect to work, but which does not: controller.java:

@FXML Spinner<Integer> mySpinner;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {

    mySpinner = new Spinner(1, 100, 1, 1);

}

What works:

SpinnerValueFactory.IntegerSpinnerValueFactory myFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 100, 1, 1);
@FXML Spinner<Integer> mySpinner;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
    mySpinner.setValueFactory(myFactory);
}

fabian
  • 80,457
  • 12
  • 86
  • 114
parrott
  • 368
  • 4
  • 12
  • What *does not work*? What happens and what do you expect to happen? – dan1st Aug 24 '19 at 13:50
  • 3
    Annotating a field with `@FXML` means that the instance of that field will be defined in the FXML-file. Imagine the FXML-file as separate class, and the spinner from that separate class gets injected into your class. – Turing85 Aug 24 '19 at 13:52
  • @Turing85 Uhmm ... Ah, ok I see. Thank you for the explanation. And when call `.setValueFactory()` inside `initialize()` I overwrite the instance from .FXML? Is there then any way to use the constructor to install the ValueFactory on instantiation in the .FXML file? – parrott Aug 26 '19 at 05:48

0 Answers0