How to connect a listener to a JavaFX counter?

I came across what seems (to me, anyway) to be a strange problem with JavaFX spinners and the inability to connect any listener to it.

I'm used to Swing programming, where I can connect ChangeListener to JSpinner and receive events this way, but JavaFX doesn't seem to have anything like this.

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() + "!!"); } }); 

Arrows increase and decrease the value in the field, but do not affect the value in the model. Just selecting the contents of the field and pressing enter will actually update the data in the model and print the value. (Pressing the enter button is in the documentation, I know.)

I also understand that I put this EventHandler in a Spinner TextField using getEditor, but I have yet to see another way to do this.

Is there any way to connect the listener to the buttons of the counter? (Damn, is there even a way to get to these buttons to attach a listener?)

Am I getting the wrong type event from a counter / editor? Can I put my listener on spinnerValueFactory?
Is there some obvious solution that I don't notice here?

I will replace this with JSpinner if necessary, but it seems crazy to me that this API will have a spinner component and such an awkward way to use it.

Thanks in advance.

+7
java swing javafx
source share
1 answer

Everything seems to work fine:

 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 error , which is fixed in 1.8.0u60.

+11
source share

All Articles