Reputation: 287
I'm trying to get value of Spinner(JavaFX). But return null.
Source:
public class FXMLTeste implements Initializable {
@FXML
private Spinner uni;
@Override
public void initialize(URL url, ResourceBundle rb) {
uni.setEditable(true);
}
@FXML
public void actionButton(){
System.out.println(uni.getValue()); // return NULL
}
}
<Spinner fx:id="uni" prefHeight="25.0" prefWidth="342.0"></Spinner>
Upvotes: 0
Views: 969
Reputation: 82491
You need to set the valueFactory
for the spinner to allow the Spinner
's value to become non-null
. This can be done by directly assigning the property:
<Spinner fx:id="uni" prefHeight="25.0" prefWidth="342.0">
<valueFactory>
<!-- add factory instance creation here -->
</valueFactory>
</Spinner>
by using the appropriate constructor
<Spinner fx:id="uni" prefHeight="25.0" prefWidth="342.0" min="1" max="10" initialValue="4" />
or by "manually" doing it in the initialize
method:
SpinnerValueFactory valueFactory = ...
uni.setValueFactory(valueFactory);
Also you should specify the type parameter for the Spinner
, even if it's a wildcard (Spinner<?>
), see What is a raw type and why shouldn't we use it?
Making an educated guess, you may be looking for something like this:
<fx:define>
<FXCollections fx:factory="observableArrayList" fx:id="unis" />
</fx:define>
<Spinner fx:id="uni" prefHeight="25.0" prefWidth="342.0" items="$unis" />
Controller
@FXML
private Spinner<String> uni;
@FXML
private ObservableList<String> unis;
@Override
public void initialize(URL url, ResourceBundle rb) {
unis.add("Oxford");
unis.add("Harvard");
}
Upvotes: 1