Re: jslider mouse click
To: comp.lang.java.gui
In article
<cc1aaac0-2db5-469b-8f28-207d065d9ac1@g17g2000prg.googlegroups.com>,
caultonpos <caultonpos@gmail.com> wrote:
On Nov 10, 7:27apm, "John B. Matthews" <nos...@nospam.invalid> wrote:
In article
<d0a83de9-fe90-4ccc-a9b4-9ee1ca4ea...@d42g2000prb.googlegroups.com>,
acaultonpos <caulton...@gmail.com> wrote:
[...]
Java 1.6 Is there any way I can setup a mouse listener to set the
value of the slide to be the nearest tick to where the user clicked -
instead of just incrementing one unit up or down?
[...]
See setSnapToTicks():
<http://java.sun.com/javase/6/docs/api/javax/swing/JSlider.html>
[Please trim signatures.]
I have snaptoticks on - it still only moves one unit at a time
regardless of where you click on the slider
The field is named snapToTicks; it is controlled by the setSnapToTicks()
method; the listener is irrelevant. If you were to supply an sscce
<http://pscode.org/sscce.html>, it might be possible to identify your
problem. What is the behavior of the following program on your
implementation?
<sscce>
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
/**
* JSliderTest1
* @author John B. Matthews
*/
public class JSliderTest1 extends JPanel {
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Sliders!");
frame.add(new JSliderTest1());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 400);
frame.setVisible(true);
}
});
}
public JSliderTest1() {
this.setLayout(new BorderLayout());
this.add(genVerticalSliderPanel(), BorderLayout.WEST);
this.add(genHorizontalSliderPanel(1), BorderLayout.NORTH);
this.add(genHorizontalSliderPanel(3), BorderLayout.CENTER);
this.add(genHorizontalSliderPanel(1), BorderLayout.SOUTH);
this.add(genVerticalSliderPanel(), BorderLayout.EAST);
}
private JPanel genHorizontalSliderPanel(int count) {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for (int i = 0; i < count; i++) {
JSlider slider = new JSlider(JSlider.HORIZONTAL);
decorate(slider, 400, Short.MAX_VALUE);
panel.add(Box.createVerticalGlue());
panel.add(slider);
}
panel.add(Box.createVerticalGlue());
return panel;
}
private JPanel genVerticalSliderPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JSlider slider = new JSlider(JSlider.VERTICAL);
decorate(slider, Short.MAX_VALUE, 300);
panel.add(slider);
return panel;
}
private void decorate(JSlider slider, int w, int h) {
slider.setMaximumSize(new Dimension(w, h));
slider.setSnapToTicks(true);
slider.setMajorTickSpacing(20);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
}
}
</sscce>
--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24