I've run into what seems (to me, anyway) an odd problem with JavaFX spinners and not being able to attach any kind of listener to it.
I'm used to Swing programming, where I can attach a ChangeListener to a JSpinner and receive events that way, but JavaFX doesn't seem to have anything like that.
The code in question...
IntegerSpinnerValueFactory spinnerValueFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(0, Integer.MAX_VALUE);
hullPointsSpinner = new Spinner<Integer>(spinnerValueFactory);
hullPointsSpinner.setEditable(true);
((TextField)hullPointsSpinner.getEditor()).setOnAction(new EventHandler<ActionEvent>() {
public void handle( ActionEvent event )
{
System.out.println("Howdy, folks! Value is " + hullPointsSpinner.getValue() + "!!");
}
});
The arrow buttons will increase and decrease the value in the field, but have no effect on the value in the model. Only selecting the contents of the field and pressing enter will actually update the data in the model and prints out the value. (The pressing enter thing is in the documentation, I know.)
I also realize that I'm putting that EventHandler onto the Spinner's TextField with getEditor, but I have yet to see another way to do this.
Is there a way to hook a listener to the spinner's buttons?
(Heck, is there even a way to get at those buttons to attach a listener?)
Am I receiving the wrong type event from the spinner/editor?
Can I put some kind of listener on the spinnerValueFactory?
Is there some kind of obvious solution that I'm overlooking here?
I'll replace this with a JSpinner, if necessary, but it just seems crazy to me that this API would have a spinner component and such an awkward way to use it.
Thanks in advance.
The following seems to work fine for me:
hullPointsSpinner.valueProperty().addListener((obs, oldValue, newValue) ->
System.out.println("New value: "+newValue));
Alternatively you can do
spinnerValueFactory.valueProperty().addListener(...);
with the same listener as above.
You should note this bug, which is fixed in 1.8.0u60.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With