3

I am using several horizontally positioned JSlider objects in my Java application and would like to align their left and right track ends vertically. That means whenever two knobs are positioned on the left end of their track the upper knob should be exactly above the lower one without any displacement in horizontal direction.

The problem I currently have is that long labels at ticks on the left or right end of a track decrease the length of the track and do not allow a nice alignment. As an example, see the following image:

JSlider alignment problem example

Does anyone know how to vertically align the tracks of two horizontally positioned JSlider objects independent of their tick labels?

A solution for my problem might be to left-align/right-align labels at the end of the track (i.e. the label on the far left tick is left-aligned to its tick), but I did not find out how I could do that. Maybe there is also a better solution I did not think about.

Thanks, Sandreal

Sandreal
  • 33
  • 3

1 Answers1

1

I do not think that the JSlider labels are accessible in such a way that you can edit their location, so instead you should create separate labels and position then yourself relative to the JSlider, then you can be sure that they will not effect the JSlider (depending on your layout manager).

sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • Good point! Never thought about that... I will try it and give you some feedback if it's easy to do it like that or not. – Sandreal May 04 '17 at 11:39
  • Yes, it works very well, thank you! I set slider.setPaintLabels to false and added labels to the desired locations. I think GroupLayout is a good choice for easy positioning. – Sandreal May 04 '17 at 13:42