2

我们可以通过在 JavaFX 中修改Slider的MinorMajorTicks来创建预先创建的Minor 和 MajorTicks。但我想让用户只能选择我预先配置的值,比如. 我们有snapToTicks来解决一个问题,但是2, 4, 8, 16, 32

如何仅启用特定刻度线或禁用其他刻度线?

我可能只能从valueProperty中过滤出想要的值,但是是否有更智能的解决方案或本地方法?


由于 JavaFX 2.2 中的一个错误,无法格式化 SliderClass 中的标签,因此您需要使用 JavaFX 8。这些值仍然可能被计算出来,只是显示错误。

我为 JavaFX 2.2 添加了一个修改版本

功能滑块.java

公共类 FunctionalSlider 扩展 Slider { private ReadOnlyDoubleWrapper functionValue = new ReadOnlyDoubleWrapper();

public FunctionalSlider() {
    this.valueProperty().addListener(new ChangeListener<Number>() {
        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            functionValue.set(Math.pow(2, getValue()));
        }
    });

    this.setLabelFormatter(new StringConverter<Double>() {
        @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();
}

}

功能滑块示例

public class FunctionalSliderSample extends Application {
    private final ListView<String> startLog = new ListView<String>();
    private final ListView<String> endLog   = new ListView<String>();

    @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<Boolean>() {

            @Override
            public void changed(ObservableValue<? extends Boolean> 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<String> 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); }
}
4

1 回答 1

2

这是基于早期示例代码的答案:JavaFX 2.2: Hooking Slider Drag n Drop Events

滑块值更改记录器

该示例使用一个将DoubleFunction作为参数的类扩展了Slider 。使用滑块labelFormatter创建自定义刻度标签的应用程序。还为属性提供值,该属性表示在给定刻度线处评估的函数的值。该代码使用Java 8FunctionalSliderDoubleFunctionDoubleFunctionfunctionValue

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<Double> function) {
    valueProperty().addListener(observable ->
        functionValue.set(
            function.apply(getValue())
        )
    );

    setLabelFormatter(new StringConverter<Double>() {
      @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<String> startLog = new ListView<>();
    private final ListView<String> endLog   = new ListView<>();

    @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 -> 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 -> {
            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<String> 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); }
}
于 2013-09-20T06:23:21.307 回答