Question

I'm attempting to implement a changelistener on a JSlider. I've tried two separate methods neither has worked. The commented out section is the first attempt. The one that's implemented now would probably be the better suited for my purposes. Can anyone point out what is going wrong with this:

public class MixWindow extends JFrame implements ChangeListener{

    private JPanel contentPane;
    public static int uniA [] = new int [512];
    ChangeListener sizeAction;
    int level = 0;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MixWindow frame = new MixWindow();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        initUni();
    }

    public MixWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 600);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JSlider slider = new JSlider(0,255);
        slider.setOrientation(SwingConstants.VERTICAL);
        slider.setBounds(66, 275, 72, 140);
        slider.setPaintTicks(true);
        slider.setMajorTickSpacing(20);
        slider.setValue(uniA[0]);
        //slider.addChangeListener(sizeAction);
        contentPane.add(slider);


        final JLabel label = new JLabel("");
        label.setBounds(66, 262, 61, 16);
        contentPane.add(label);


        /*sizeAction=new ChangeListener()
        {
            public void stateChanged (ChangeEvent event)
            {
                System.out.println("This is getting silly");
                JSlider slider=(JSlider)event.getSource();

                level=slider.getValue();
                uniA[0] = level;
                String temp = String.valueOf(level); 
                label.setText(temp);



            }
        };*/
    }

    public static void initUni(){
        for(int i = 0; i < uniA.length; i++){
            uniA[i] = 0;
          }
    }

    @Override
    public void stateChanged(ChangeEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("Stuff has changed"); 
        Object source = arg0.getSource();
        System.out.println(arg0 + " has Changed");

    }
}
Was it helpful?

Solution

The reason the ChangeListener isn't working in your 1st approach, is that your listener reference sizeAction is null when you register the listener.

slider.addChangeListener(sizeAction);

While this won't throw an exception, it won't register the listener when it is instantiated. Simply alllow this line to appear after you define the listener and it will start working. If you wish to use your other ChangeListener implementation instead you can use:

slider.addChangeListener(this);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top