Javafx custom slider values

Viewed 277

I'm trying to use a JavaFX slider to choose some time instants to visualize the relative graph in my application. So I should put a list of double values in the slider range, and they always change because I get them from external files. For example: I have as instants: 0.0, 1.9, 2.5, 50.1, 98.7 and so on. I don't want to use default ticks but I want to show only the values that I really have. I haven't found anything in the web.

1 Answers

One option that I can think of is to have the actual value of the slider be the index of the real value you want. Use a formatter to show the real value. You would need to convert the slider value (the index) to the actual value when you process it.

Here's a quick example:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;


/**
 * JavaFX App
 */
public class App extends Application {

    @Override
    public void start(Stage stage) {
        double[] timepoints = {0.0, 1.9, 2.5, 50.1, 98.7};
        
        Slider slider = new Slider();
        slider.setMin(0);
        slider.setMax(timepoints.length - 1);
        slider.setSnapToTicks(true);
        slider.setMajorTickUnit(1);
        slider.setMinorTickCount(0);
        slider.setShowTickMarks(true);
        slider.setShowTickLabels(true);
        slider.setLabelFormatter(new StringConverter<Double>() {

            @Override
            public String toString(Double object) {
                int index = object.intValue();
                return String.valueOf(timepoints[index]);
            }

            @Override
            public Double fromString(String string) {
                // Not used
                return null;
            }
            
        });
        
        Label label = new Label();
        slider.valueProperty().addListener((obs, oldValue, newValue) -> {
            label.setText(String.valueOf(timepoints[newValue.intValue()]));
        });
        
        VBox root = new VBox(10, label, slider);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));
        Scene scene = new Scene(root, 400, 400);
        
        
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

If you need a lot of this, you could factor it out into a separate class (this is not production-quality, but will give you the idea):

import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.Control;
import javafx.scene.control.Slider;
import javafx.util.StringConverter;

public class DiscreteSlider<T> {

    private final ObservableList<T> values ;
    private final ObjectProperty<T> discreteValue = new SimpleObjectProperty<>();
    
    private final Slider slider ;
    
    public DiscreteSlider(ObservableList<T> values) {
        this.values = values ;
        this.slider = new Slider() ;
        
        slider.setMin(0);
        slider.maxProperty().bind(Bindings.size(this.values).subtract(1));
        slider.setShowTickLabels(true);
        slider.setMajorTickUnit(1);
        slider.setMinorTickCount(0);
        slider.setSnapToTicks(true);
        slider.labelFormatterProperty().bind(Bindings.createObjectBinding(() -> new StringConverter<Double>() {

            @Override
            public String toString(Double object) {
                int index = object.intValue();
                return DiscreteSlider.this.values.get(index).toString();
            }

            @Override
            public Double fromString(String string) {
                // Not used
                return null;
            }
            
        }, this.values));
        
        discreteValue.addListener((obs, oldValue, newValue) -> {
            slider.setValue(this.values.indexOf(newValue));
        });
        
        slider.valueProperty().addListener((obs, oldValue, newValue) -> {
            int index = newValue.intValue();
            setDiscreteValue(values.get(index));
        });
        
        if (values.size() > 0) setDiscreteValue(values.get(0));
    }
    
    @SafeVarargs
    public DiscreteSlider(T... values) {
        this(FXCollections.observableArrayList(values));
    }
    
    public ObjectProperty<T> discreteValueProperty() {
        return discreteValue ;
    }
    
    public final T getDiscreteValue() {
        return discreteValueProperty().get();
    }
    
    public final void setDiscreteValue(T value) {
        discreteValueProperty().set(value);
    }
    
    public Control asControl() {
        return slider ;
    }
}

and then

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;


public class App extends Application {

    @Override
    public void start(Stage stage) {
        Double[] timepoints = {0.0, 1.9, 2.5, 50.1, 98.7} ;
        
        DiscreteSlider<Double> slider = new DiscreteSlider<>(timepoints);
        
        Label label = new Label();
        label.textProperty().bind(slider.discreteValueProperty().asString());
        
        VBox root = new VBox(10, label, slider.asControl());
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));
        Scene scene = new Scene(root, 400, 400);
        
        
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}
Related