Reputation: 21
I'm a new JavaFX programmer and my question is I'm getting multiple decimal points in the slider and I only want one. I'm getting like 99.99999999999 for each value within but I would love to get only one like 99.9
The thing is printf is not applicable here I guess and I only know that way >.<
private void createView() {
final GridPane radioGridPane = new GridPane();
final Label frequencyLabel = new Label("something");
final Slider sliderFrequency = new Slider(87.9, 107.9, 90);
frequencyLabel.textProperty().bind(sliderFrequency.valueProperty().asString());
radioGridPane.add(frequencyLabel, 2, 1);
radioGridPane.add(sliderFrequency, 3, 1);
this.setCenter(radioGridPane);
radioGridPane.setGridLinesVisible(true);
}
Upvotes: 1
Views: 470
Reputation: 13918
You were actually quite close. You can use asString method with the following signature:
javafx.beans.binding.NumberExpressionBase#asString(java.lang.String)
that accepts a format parameter.
Example usage:
public class FormatBindingSSCCE extends Application {
@Override
public void start(Stage stage) {
final Label frequencyLabel = new Label();
final Slider sliderFrequency = new Slider(87.9, 107.9, 90);
frequencyLabel.textProperty().bind(sliderFrequency.valueProperty().asString("%.1f"));
final GridPane radioGridPane = new GridPane();
radioGridPane.add(frequencyLabel, 2, 1);
radioGridPane.add(sliderFrequency, 3, 1);
final Scene scene = new Scene(radioGridPane);
stage.setScene(scene);
stage.show();
}
}
Upvotes: 4