java - Changing JSlider value to JSpinner value and vice versa on change -


i have jslider object named quantitysl , jspinner object named quantitysp. when changes spinner want change slider same value , vice versa.

the initialization of objects follows:

spinnernumbermodel quantitylimiter = new spinnernumbermodel(1, 0, 20, 1);  jspinner quantitysp = new jspinner(quantitylimiter); jslider quantitysl = new jslider(0,20,1); quantitysl.addchangelistener(this); quantitysp.addchangelistener(this); 

here code i'm trying change values:

@override public void statechanged(changeevent ce) {         if(ce.getsource()==quantitysp){             quantitysp.setvalue(quantitysl.getvalue());         }         else if(ce.getsource()==quantitysl){             quantitysl.setvalue((int) quantitysp.getvalue());         } } 

the problem changing value of spinner triggers statechanged event results in locking of both input fields. how can solve this?

try add separate listener each component this:

import java.awt.borderlayout; import javax.swing.jframe; import javax.swing.jslider; import javax.swing.jspinner; import javax.swing.spinnernumbermodel; import javax.swing.swingutilities; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.event.changeevent; import javax.swing.event.changelistener;  @suppresswarnings("serial") public class main extends jframe {     private jslider slider;     private jspinner spinner;     private int min, max;      public main()     {         setdefaultcloseoperation(jframe.exit_on_close);         setbounds(100, 100, 400, 200);         initializecomponents();         setvisible(true);     }      public void initializecomponents()     {         min = 0;         max = 100;         slider = new jslider(jslider.horizontal, min, max, min);         slider.setmajortickspacing(10);         slider.setminortickspacing(1);         slider.setpaintticks(true);         slider.setpaintlabels(true);         spinner = new jspinner(new spinnernumbermodel(min, min, max, 1));          slider.addchangelistener(new changelistener()         {             @override             public void statechanged(changeevent e)             {                 spinner.setvalue(slider.getvalue());             }         });          spinner.addchangelistener(new changelistener()         {             @override             public void statechanged(changeevent e)             {                 slider.setvalue((int) spinner.getvalue());             }         });          add(slider, borderlayout.north);         add(spinner, borderlayout.south);     }      public static void main(string[] args)     {         swingutilities.invokelater(new runnable()         {             public void run()             {                  try                 {                     uimanager.setlookandfeel("javax.swing.plaf.nimbus.nimbuslookandfeel");                 }                 catch (classnotfoundexception | instantiationexception                         | illegalaccessexception                         | unsupportedlookandfeelexception e)                 {                 }                 new main();             }         });     } } 

Comments