Not getting value from JSlider

穿精又带淫゛_ 提交于 2019-12-11 04:50:52

问题


I am using a JSlider in my program, and have implemented a ChangeListener for the same.

public void stateChanged(ChangeEvent e)
    {
        JSlider source=(JSlider) e.getSource();
        frame_value.setText(Integer.toString(source.getValue()));

        //Condition to change the frame_no only when user has stopped moving the slider
        if (!source.getValueIsAdjusting()) 
        {
            frame_no=(int) source.getValue()-1;
            if(frame_no<0)
                frame_no=0;
        }
        ....
}

What is happening is, that whenever the ChangeListener is called, the program just skips the if block, and goes to the code after that. I don't understand why is this happening. I am not able to get the correct value from the JSlider. Please help!!

PS: I don't know if this is the reason, but recently I have set the UI of the JSlider to place the tick where I click it. I don't know if that is responsible for it or not. Here is the code:

slider.setUI(new MetalSliderUI() {
                protected void scrollDueToClickInTrack(int direction) {
                    int value = HEVC_Analyzer.slider.getValue(); 

                   value = this.valueForXPosition(HEVC_Analyzer.slider.getMousePosition().x);
                   HEVC_Analyzer.slider.setValue(value);
                }
            });

回答1:


Must be something wrong in your code, since it's working fine in this example :

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderChangeEffect extends JFrame
{
    private JSlider slider;
    private int count = 0;
    private ChangeListener changeListener = new ChangeListener()
    {
        public void stateChanged(ChangeEvent ce)
        {
            JSlider slider = (JSlider) ce.getSource();
            if (!slider.getValueIsAdjusting())
                System.out.println(slider.getValue());
        }
    };

    private void createAndDisplayGUI()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        JPanel contentPane = new JPanel();
        contentPane.setOpaque(true);

        slider = new JSlider(0, 10, 5);
        slider.setMajorTickSpacing(2);
        slider.setMinorTickSpacing(1);
        slider.addChangeListener(changeListener);

        contentPane.add(slider);

        getContentPane().add(contentPane);
        pack();
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new SliderChangeEffect().createAndDisplayGUI();
            }
        });
    }
}



回答2:


Just use the String.valueOf() and the event.MouseReleased().

 private void jSlider1MouseReleased(java.awt.event.MouseEvent evt) {                                       
    // TODO add your handling code here:
    try {

        String valueOf = String.valueOf(jSlider1.getValue());
        jLabel1.setText(valueOf);

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}   


来源:https://stackoverflow.com/questions/11153621/not-getting-value-from-jslider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!