Reputation: 16255
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?
Upvotes: 1
Views: 4115
Reputation: 5349
This is how i accomplish the MouseClickEvent
MoveSlider = new JSlider(JSlider.HORIZONTAL, 0, 0, 0);
MoveSlider.addMouseListener(new MouseListener()
{
public void mousePressed(MouseEvent event) {
//Mouse Pressed Functionality add here
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
});
Upvotes: 1
Reputation: 2477
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.
Upvotes: 4
Reputation: 324118
I think you need to write a custom UI for this. This should get you started:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
public class SliderScroll extends JFrame
{
public SliderScroll()
{
final JSlider slider = new JSlider(0, 50, 20);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(5);
slider.setExtent(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
getContentPane().add( slider );
slider.setUI( new MySliderUI() );
}
class MySliderUI extends MetalSliderUI
{
public void scrollByUnit(int direction)
{
synchronized(slider)
{
int oldValue = slider.getValue();
int delta = (direction > 0) ? 10 : -5;
slider.setValue(oldValue + delta);
}
}
}
public static void main(String[] args)
{
JFrame frame = new SliderScroll();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
}
Upvotes: 2