1

While implementing an event handler for a slider in JavaFX and FXML, I was not able to figure out what event type a slider fires. I searched the Javadoc and googled it, but finally solved it by trial-and-error: a mouse released event can track when the user has changed the value of the slider (my first guess was some kind of ValueChangeListener or ChangeListener like in swing).

Question: How can I know which event listener/handler goes with which control? If the API/Javadoc does not state that slider value changes preferably are handled by mouse events, where to look for that information?

NoJoshua
  • 374
  • 3
  • 11
  • You can listen directly to changes in the [`value` property](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Slider.html#valueProperty). – James_D Dec 18 '15 at 12:45
  • In addition to the value property on slider there is also a valueChanging property which is discussed in this Q&A: [JavaFX 2.2: Hooking Slider Drag n Drop Events](http://stackoverflow.com/questions/18892070/javafx-2-2-hooking-slider-drag-n-drop-events) – jewelsea Dec 18 '15 at 19:38

2 Answers2

1

JavaFX replaced most value events by Properties, since they are far more flexible. So when you wonna keep track of the current value of a component, the easiest approach is to add a ChangeListener in your case to the valueProperty of a Slider:

slider.valueProperty().addListener( ( ov, oldValue, newValue ) ->
{
  System.out.println( "OldValue = " + oldValue + " newValue = " + newValue );
} );

For a more detailed explanation with example have a look here.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
crusam
  • 6,140
  • 6
  • 40
  • 68
0

You may add a super class event (i.e. the most top class Event, and not specific event like ActionEvent) filter (not handler) to some parent layout (or to scene) and observe what kind concrete events are propagating through.

The other option is to simply read the source code :)

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • There is nothing in the source code about event type. From @crusam's answer I understand that I would rather read some document on differences between java.swing and javafx (which is a fair deal). – NoJoshua Dec 18 '15 at 14:03
  • @crusam's answer is totally valid. There are `property`s for the node in JavaFX whcih you can add change listener or bind to other nodes' properties, but there are also events fired from node as a source to some destination node. For latter ones you can attach an event filter or handler (you may also prefer to read the difference of both). – Uluk Biy Dec 18 '15 at 14:13