java - JSlider snapping to every 90th number -
my jframe window has system , feel , when add jslider , click on line, goes 90 steps forward or backward depending on mouse position. want diamond go under mouse , work if clicked , dragged on diamond.
here minimal program think happens in windows because in default , feel, didnt snap 90th numbers went ahead 1 number.
import java.awt.borderlayout; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jslider; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.event.changeevent; import javax.swing.event.changelistener; @suppresswarnings("serial") public class test extends jframe { int value = 300; jslider slider; jlabel label; public test() { setsize(300, 200); setdefaultcloseoperation(exit_on_close); setlocationrelativeto(null); label = new jlabel(string.valueof(value)); slider = new jslider(); slider.setminimum(100); slider.setmaximum(1000); slider.setvalue(value); slider.setsnaptoticks(false); slider.addchangelistener(new changelistener() { public void statechanged(changeevent e) { label.settext(string.valueof(slider.getvalue())); } }); add(label, borderlayout.north); add(slider); setvisible(true); } public static void main(string[] args) throws classnotfoundexception, instantiationexception, illegalaccessexception, unsupportedlookandfeelexception { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); new test(); } }
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.basic.*; public class slidermousepressed extends jpanel { public slidermousepressed() { jslider slider = new jslider(0, 50, 20); slider.setmajortickspacing(10); slider.setminortickspacing(5); slider.setpaintticks(true); slider.setpaintlabels(true); add(slider); snaptomouseclick( slider ); } public static void snaptomouseclick(final jslider slider) { mouselistener[] ml = slider.getmouselisteners(); (mouselistener l : ml) slider.removemouselistener(l); mousemotionlistener[] mml = slider.getmousemotionlisteners(); (mousemotionlistener l : mml) slider.removemousemotionlistener(l); final basicsliderui ui = (basicsliderui)slider.getui(); basicsliderui.tracklistener tl = ui.new tracklistener() { // position slider @ mouse @override public void mouseclicked(mouseevent e) { point p = e.getpoint(); int value = ui.valueforxposition(p.x); slider.setvalue(value); } // prevent scrolling while mouse button held down @override public boolean shouldscroll(int dir) { return false; } }; slider.addmouselistener(tl); slider.addmousemotionlistener(tl); } private static void createandshowui() { jframe frame = new jframe("slidermousepressed"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add( new slidermousepressed() ); frame.setlocationbyplatform( true ); frame.pack(); frame.setvisible( true ); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { createandshowui(); } }); } }
this example based on answer slight modification remove/add mousemotionlistener mouselistener make painting of thumb slider more consistent default behaviour. thumb painted @ same relative position mouse instead of @ left, when thumb dragged.
Comments
Post a Comment