Get access to Thumb/Handle/Knob of Swing's JSlider
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);
精彩评论