Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaFX 2.2 configuring Slider ticks manually
    primarykey
    data
    text
    <p>We can create pre-created Minor and Major Ticks by modifying the <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/control/Slider.html#minorTickCountProperty" rel="nofollow">Minor</a> and <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/control/Slider.html#majorTickUnitProperty" rel="nofollow">MajorTicks</a> of the <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/control/Slider.html" rel="nofollow">Slider</a> in JavaFX. But I want to enable the user only to select pre-configured values by me like <code>2, 4, 8, 16, 32</code>. We have <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/control/Slider.html#snapToTicksProperty" rel="nofollow">snapToTicks</a> to solve one problem, but </p> <p>How do I enable only specific tick marks or disable the others?</p> <p>I could probably filter only the wanted values out from <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/control/Slider.html#valueProperty%28%29" rel="nofollow">valueProperty</a> but is there either a smarter solution or a way to do it natively?</p> <hr> <p>Due to a bug in JavaFX 2.2 it is not possible to format the labels in the SliderClass, thus you need to use JavaFX 8. The values are still calculated probably and just displayed wrongly.</p> <p>I add a modified version for JavaFX 2.2 </p> <p><em>FunctionalSlider.java</em></p> <p>public class FunctionalSlider extends Slider { private ReadOnlyDoubleWrapper functionValue = new ReadOnlyDoubleWrapper();</p> <pre><code>public FunctionalSlider() { this.valueProperty().addListener(new ChangeListener&lt;Number&gt;() { @Override public void changed(ObservableValue&lt;? extends Number&gt; observable, Number oldValue, Number newValue) { functionValue.set(Math.pow(2, getValue())); } }); this.setLabelFormatter(new StringConverter&lt;Double&gt;() { @Override public Double fromString(String string) { return 0.0; } @Override public String toString(Double object) { return String.format("%1$.0f", Math.pow(2, object)); } }); } public double getFunctionValue() { return functionValue.get(); } public ReadOnlyDoubleProperty functionValueProperty() { return functionValue.getReadOnlyProperty(); } </code></pre> <p>}</p> <p><em>FunctionalSliderSample</em></p> <pre><code>public class FunctionalSliderSample extends Application { private final ListView&lt;String&gt; startLog = new ListView&lt;String&gt;(); private final ListView&lt;String&gt; endLog = new ListView&lt;String&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(); 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(new ChangeListener&lt;Boolean&gt;() { @Override public void changed(ObservableValue&lt;? extends Boolean&gt; observable, Boolean oldValue, Boolean newValue) { slider.setValue(Math.round(slider.getValue())); String valueString = String.format("%1$.0f", 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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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