문제

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.

도움이 되었습니까?

해결책

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);
  }
});

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top