2

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.

Chantz
  • 5,883
  • 10
  • 56
  • 79
  • what exactly is the problem? While there's no direct api to get the thumbPosition, you can calculate (at least approximately) by simple geometry .. – kleopatra Jun 15 '11 at 09:40

2 Answers2

3

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);
  }
});
Howard
  • 38,639
  • 9
  • 64
  • 83
  • possible, but typically not a good idea - maintain several (one per LAF) ui implementations over jdk versions is not exactly fun ;-) – kleopatra Jun 15 '11 at 08:52
  • So now there is a new problem. I get the values of thumb alright but when I try to set the location of JLabel that shows current value, it starts jumping all over the place & produces a flickering effect. – Chantz Jun 15 '11 at 18:18
  • @Chantz - jumping when doing what exactly? Time for a short runnable (blabla) code example – kleopatra Jun 15 '11 at 22:18
  • @kleopatra sorry for the delayed response. Actually I ended up junking the feature. Although I will try to come up with SSCCE ASAP. – Chantz Jun 21 '11 at 20:48
1

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);
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • was wondering why the property had to be added to the LAFDefaults - should work as well with a simple UIManager.put - looks like a bug that's fixed in 1.7 (so there _had_ been bug fixes to Nimbus ;-) – kleopatra Jun 15 '11 at 08:50
  • @kleopatra: Thanks for addressing this; I modified the defaults empirically, without really understanding why `UIManager.put()` failed. Is [this](http://bugs.sun.com/view_bug.do?bug_id=6653395) the bug? – trashgod Jun 15 '11 at 19:21
  • 1
    don't know - simply noticed that putting to lookandfeelDefaultswas necessary in 1.6 and UIManager.put suffised in 1.7 – kleopatra Jun 15 '11 at 22:16