0

I am trying to code my way into displaying in a label(like a tooltip) the current value of a position in a slider when I drag the slider thumb. For example: if a slider has Integer values from 0 to 100, and I drag the slider thumb to the middle of the slider, a small popup will show me that I am currently in position 50.

So far, that's what I managed to do:

final Label textLabel = new Label("Volume");
    final Slider volumeSlider = new Slider();

    volumeSlider.valueProperty().addListener(new ChangeListener() {

        @Override
        public void changed(ObservableValue arg0, Object arg1, Object arg2) {
            textLabel.textProperty().setValue(
                    String.valueOf((int) volumeSlider.getValue()));

        }
    });

    root.getChildren().addAll(volumeSlider, textLabel);
    textLabel.textProperty().setValue("abc");

It doesn't display on screen the values.

Also, if any of you happen to know, how do I set a starting point in a slider track? Like, if I wanted the program to start and the slider thumb to start in the middle of the slider(value 50) instead of starting on the lowest part.

Coder
  • 9
  • 3
  • To set the initial value to 50, consider calling `setValue(50);`. – James_D Feb 28 '17 at 02:34
  • If I don't make the conversion the values displaying will be way too accurate for what I want. – Coder Feb 28 '17 at 02:34
  • Sorry, misread the question. – James_D Feb 28 '17 at 02:35
  • Your comment provided the solution for my second problem. Indeed, `setValue(50)` does work. I thought there was an specific method for this job. – Coder Feb 28 '17 at 02:37
  • Are you looking for something like [this](http://stackoverflow.com/questions/38209981/value-into-slider-thumb-in-javafx)? And then there's [this](http://stackoverflow.com/questions/34036328/javafx-slider-value-at-mousemoved-event) somewhat longer and more involved discussion. – James_D Feb 28 '17 at 02:45
  • @James_D the first method you mentioned left me unable to load media into my stage. The MediaView view is blank. I'm looking through the other link. To make things clearer, I am looking for way to display the exact position value of the thumb when dragged by the mouse on the track. To give the user feedback. – Coder Feb 28 '17 at 03:00
  • are you able to `System.out.println(String.valueOf((int) volumeSlider.getValue()));` to see if it even outputting the value? if it isn't then you may need to do something like `textLabel.textProperty().setValue(Double.toString(volumeSlider.getValue());` – TravisF Feb 28 '17 at 03:38
  • It should be printing value because I tested this code in another class from a different project and it worked. Which leads me to believe that something is blocking the output from view. – Coder Feb 28 '17 at 17:14

0 Answers0