Search code examples
javaswingjslider

Jslider should fire change event when it come to rest


I have one slider in my JAVA application.I have written change listener for that slider. Here is the code i have written

jSlider = new JSlider(JSlider.HORIZONTAL,0,30,2);
        jSlider.setFont(new Font("Dialog", Font.BOLD, 10));
        jSlider.setMinorTickSpacing(1);
        jSlider.setMajorTickSpacing(2);
        jSlider.setPaintTicks(true);
        jSlider.setPaintLabels(true);
        jSlider.setBounds(76, 564, 586, 55);
        jSlider.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent arg0) {
                // TODO Auto-generated method stub
                textField.setText(String.valueOf(jSlider.getValue()));
            }
        });
        getContentPane().add(jSlider);  

This code gives the continuous changing values of the slider.

but i want the value of rest position of the slider. What should I write to get the value only for the rest position?


Solution

  • We can also use

    jSlider.addChangeListener(new ChangeListener() {
    
                @Override
                public void stateChanged(ChangeEvent e) {
                    // TODO Auto-generated method stub
                    JSlider source = (JSlider)e.getSource();
                    if(!source.getValueIsAdjusting())
                    {
                        //textField.setText(String.valueOf(source.getValue()));
                        int gain = source.getValue();
                        //System.out.println("***** GAIN ***** "+gain);
                        GetGain g = new GetGain(gain);
                    }
                }
            });
    

    Above code also gives the value for the rest position of the slider.