I am using a JSlider
in my application. I want only left click to slide JSlider
, and I want to disable right click for JSlider
. I am able to get event on right click, but slider is changing its value to other position.
jSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
//code
}
});
jSlider.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if (e.getButton() == java.awt.event.MouseEvent.BUTTON3) {
//code
} else {
//code
}
}
});
I am using JDK1.7.0_03, Windows 7
UIManager.put("Slider.onlyLeftMouseButtonDrag", Boolean.TRUE);
JDK1.6.0_31:
Bug ID: 6614972 JSlider value should not change on right-click
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.metal.MetalSliderUI;
public class SliderOnlyLeftMouseButtonDragTest {
public JComponent makeUI() {
JSlider slider00 = new JSlider(0,10,5);
JSlider slider01 = new JSlider(0,10,5);
slider01.setUI(new MetalSliderUI() {
protected TrackListener createTrackListener(JSlider slider) {
return new TrackListener() {
@Override public void mousePressed(MouseEvent e) {
if(!SwingUtilities.isLeftMouseButton(e)) {
return;
}
super.mousePressed(e);
}
};
}
});
JPanel p = new JPanel();
p.add(slider00);
p.add(slider01);
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new SliderOnlyLeftMouseButtonDragTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}