Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an answer based on earlier sample code from: <a href="https://stackoverflow.com/questions/18892070/javafx-2-2-hooking-slider-drag-n-drop-events">JavaFX 2.2: Hooking Slider Drag n Drop Events</a>.</p> <p><img src="https://i.stack.imgur.com/tWUiv.png" alt="slider value change logger"></p> <p>The sample extends <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/control/Slider.html" rel="nofollow noreferrer">Slider</a> with a <code>FunctionalSlider</code> class that takes a <a href="http://download.java.net/jdk8/docs/api/java/util/function/DoubleFunction.html" rel="nofollow noreferrer">DoubleFunction</a> as an argument. Applications of the <code>DoubleFunction</code> create custom tick labels using a slider <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/control/Slider.html#setLabelFormatter%28javafx.util.StringConverter%29" rel="nofollow noreferrer">labelFormatter</a>. The <code>DoubleFunction</code> also supplies values to a <code>functionValue</code> property which represents the value of the function evaluated at a given tick mark. The code uses <a href="https://jdk8.java.net/download.html" rel="nofollow noreferrer">Java 8</a>.</p> <pre><code>import javafx.application.Application; import javafx.beans.property.*; import javafx.geometry.*; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.Stage; import javafx.util.StringConverter; import java.util.function.DoubleFunction; class FunctionalSlider extends Slider { private ReadOnlyDoubleWrapper functionValue = new ReadOnlyDoubleWrapper(); public FunctionalSlider(DoubleFunction&lt;Double&gt; function) { valueProperty().addListener(observable -&gt; functionValue.set( function.apply(getValue()) ) ); setLabelFormatter(new StringConverter&lt;Double&gt;() { @Override public String toString(Double x) { return String.format( "%1$.0f", function.apply(x) ); } @Override public Double fromString(String s) { return null; } }); } public double getFunctionValue() { return functionValue.get(); } public ReadOnlyDoubleProperty functionValueProperty() { return functionValue.getReadOnlyProperty(); } } public class FunctionalSliderSample extends Application { private final ListView&lt;String&gt; startLog = new ListView&lt;&gt;(); private final ListView&lt;String&gt; endLog = new ListView&lt;&gt;(); @Override public void start(Stage stage) throws Exception { Pane logsPane = createLogsPane(); Slider slider = createMonitoredSlider(); VBox layout = new VBox(10); layout.setAlignment(Pos.CENTER); layout.setPadding(new Insets(10)); layout.getChildren().setAll( slider, logsPane ); VBox.setVgrow(logsPane, Priority.ALWAYS); stage.setTitle("Slider Value Change Logger"); stage.setScene(new Scene(layout)); stage.show(); } private Slider createMonitoredSlider() { final FunctionalSlider slider = new FunctionalSlider( x -&gt; Math.pow(2, x) ); slider.setMin(0); slider.setValue(1); slider.setMax(5); slider.setMajorTickUnit(1); slider.setMinorTickCount(0); slider.setShowTickMarks(true); slider.setShowTickLabels(true); slider.setSnapToTicks(true); slider.setMinHeight(Slider.USE_PREF_SIZE); slider.valueChangingProperty().addListener(observable -&gt; { slider.setValue(Math.round(slider.getValue())); String valueString = String.format( "%1$.2f", slider.getFunctionValue() ); if (slider.valueChangingProperty().get()) { startLog.getItems().add( valueString ); } else { endLog.getItems().add( valueString ); } }); return slider; } private HBox createLogsPane() { HBox logs = new HBox(10); logs.getChildren().addAll( createLabeledLog("Start", startLog), createLabeledLog("End", endLog) ); return logs; } public Pane createLabeledLog(String logName, ListView&lt;String&gt; log) { Label label = new Label(logName); label.setLabelFor(log); VBox logPane = new VBox(5); logPane.getChildren().setAll( label, log ); logPane.setAlignment(Pos.TOP_LEFT); return logPane; } public static void main(String[] args) { launch(args); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload