Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 475
Arikado Avatar asked Mar 04 '15 20:03

Arikado


People also ask

How do I specify a controller in FXML?

There are two ways to set a controller for an FXML file. The first way to set a controller is to specify it inside the FXML file. The second way is to set an instance of the controller class on the FXMLLoader instance used to load the FXML document.

Is FXML annotation required?

The @FXML annotation is required for the controller class's private member fields; otherwise, field injection would fail.

What is the full form of FXML?

FX Markup Language. In the same way that HTML is for Hypertext Markup Language, and many more acronyms that end in ML .


1 Answers

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.

like image 92
José Pereda Avatar answered Oct 02 '22 13:10

José Pereda