Search code examples
javauser-interfaceswingmouseeventjslider

getting JSlider bar to move on mouse click event


I have a JSlider which shows bet sizes (for a poker game) I am trying to achieve the effect that when a mouse click occurs the slider jumps forward by a bet amount (i.e. a big blind amount) rather than just incrementing by one. If the mouse click happens to the left of the bar i want it to decrement by a fixed amount else increment. I looked into attaching a mouse listener, but do not know how I can use the event to find out on what side of the bar the mouse was clicked.

Any ideas?


Solution

  • You just need to change your perspective on the problem.

    Don't view the clicks as being to the 'left' or 'right' (below or above) the current bet.

    Rather, you simply store the old tick, and look at what the new tick is. The difference will tell you if the user has tried to increase (positive delta) or decrease (negative delta).

    Then you can increment by your desired 'fixed bet' amount.