2

I am trying to select a coordinate from a grid using JSliders. Can I make it so when it clicks one it clicks the other as well, one is vertical while the other is horizontal? So I can drag them both at once.

What I'm trying to do is set the horizontal slider based on horizontal movements of the mouse and set the vertical slider based on vertical movements of the mouse.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Maxwell Dergosits
  • 297
  • 1
  • 4
  • 12

2 Answers2

3

Yes you can do this. Simply get the value from the JSlider that's being dragged and use it to set the BoundedRangeModel of the other JSlider.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • *"Yes you can do this."* ..but it makes no sense whatsoever. Just because we have he technology, doesn't mean we should use it. ;) – Andrew Thompson Sep 12 '11 at 05:45
  • @Andrew: I can see situations where it could be useful, perhaps controlled by a checkbox to maintain an aspect ratio or something similar. – Hovercraft Full Of Eels Sep 12 '11 at 05:47
  • OK Good point. If that is what the OP intends, suddenly this question makes sense. Can Maxwell confirm that 'maintaining the aspect ratio' (or some equally good reason) is part of the requirement? – Andrew Thompson Sep 12 '11 at 05:52
  • @Andrew: I was thinking the same as Hovercraft. May be Maxwell wants the flexibility to either change both together or one at a time. – Mohayemin Sep 12 '11 at 05:53
  • 1
    Sometimes you will need to change a slider (proportionality) based on another like in Audio Mixing applications for example. – mohdajami Sep 12 '11 at 06:25
3

What I'm trying to do is set the horizontal slider based on horizontal movements of the mouse and set the vertical slider based on vertical movements of the mouse.

Instead I would attach a mouse listener to the area itself, and match the x/y sliders to match the mouse co-ordinates. Of course, it would help to have the sliders also change the position, but independently of each other.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • no, you wouldn't (because you would miss keyboard interaction ...) you know your technical vocabulary better than the OP :-) – kleopatra Sep 12 '11 at 08:13
  • @kleopatra When I added the bit starting *"it would help.."*, guess who I was thinking of? ;) – Andrew Thompson Sep 12 '11 at 08:18
  • hach .. probably I misunderstood the question (after re-reading a couple of times) - if he's really after updating the sliders accoring to the mouse position, then my comment was rather silly :-) But then, the question makes nut much sense: there are tutorials and api doc ... – kleopatra Sep 12 '11 at 08:36
  • @kleopatra *"after re-reading a couple of times.."* It is the devoted efforts of several people that got the question even to *that* stage. ;) – Andrew Thompson Sep 12 '11 at 09:36