Question

Basically I want to display the current value pointed to by the slider handle when in motion (either via mouse or keyboard). I can easily get the current value by adding a new ChangeListener & overriding stateChanged method. But I cant seem to get the current location of the handle.

I can just bite the bullet and create a label at a constant place & update it continuously but I want to display the value just above (or below) the handle.

Was it helpful?

Solution

Not an good or very flexible solution but maybe you can implement your own SliderUI. E.g. using the already defined BasicUI you can access the field thumbRect which contains the values you need.

slider.setUI(new BasicSliderUI(slider) {
  public void paintThumb(Graphics g) {
    super.paintThumb(g);
    g.setColor(Color.black);
    g.drawString(Integer.toString(slider.getValue()), thumbRect.x, thumbRect.y + thumbRect.height);
  }
});

OTHER TIPS

If the Nimbus Look and Feel is an option, a live display of the value can be specified in the relevant UI default:

UIManager.getLookAndFeelDefaults().put("Slider.paintValue", true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top