开发者

Get access to Thumb/Handle/Knob of Swing's JSlider

开发者 https://www.devze.com 2023-03-13 10:09 出处:网络
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 & ove

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

精彩评论

暂无评论...
验证码 换一张
取 消