Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8145567

Slider: snapToTicks not honoured on changing to true

XMLWordPrintable

        To reproduce, run the example

        - move thumb between two ticks
        - press button to enable snapToTicks
        - expected: value (and with it the thumb) snapped to nearest tick
        - actual: value/thumb unchanged, snapped at the next mouse touch only

        Example:

        public class SliderSnapToTickBug extends Application {

            @Override
            public void start(Stage primaryStage) {
                Slider slider = new Slider(5, 25, 15);
                // show ticks
                slider.setShowTickMarks(true);
                slider.setShowTickLabels(true);
                slider.setMajorTickUnit(10);
                
                Label valueLabel = new Label();
                valueLabel.textProperty().bind(slider.valueProperty().asString());
                
                Button snapTicks = new Button("snap to ticks");
                snapTicks.setOnAction(e -> {
                    slider.setSnapToTicks(!slider.isSnapToTicks());
                });
                VBox root = new VBox(10, slider, valueLabel, snapTicks);
                primaryStage.setScene(new Scene(root, 500, 400));
                primaryStage.show();
            }
         
            public static void main(String[] args) {
                launch(args);
            }
            
        }

        Reason: SliderSkin doesn't listen to changes of the snapToTicks property
        Fix: add listener and adjust value when changed to true

        // in SliderSkin constructor
        registerChangeListener(slider.snapToTicksProperty(), "SNAP_TO_TICKS");

        // in handleControlProperty
        } else if ("SNAP_TO_TICKS".equals(p)) {
            if (slider.isSnapToTicks()) {
                slider.adjustValue(slider.getValue());
            }
        }

        the fix is without any risk and well bounded - so could be added at once :-)



              vadim Vadim Pakhnushev
              fastegal Jeanette Winzenburg
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: