Question

I've asked this before but I can't seem to edit it, but now I have more of an idea on how to do it but it doesn't work.

I have initialised a LabelledSlider and made the default value as int speedValue which is assigned to 1 in the constructor.

final   LabelledSlider speed = new LabelledSlider("Sim speed", 1, 7, speedValue);

Upon loading the program the slider is 1, but when I call the action listener load which assigns the value 5 to speedValue, the slider still remains 1 and does not increase when I run the program.

loadButton.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            speedValue = 1;
        }
    });

Could anyone explain why nothing is happening and possibly provide a solution? I also have action listeners for saving slider values which works perfectly well.

Thanks

Was it helpful?

Solution

You're code shows magical thinking, where you assume that when you change the value of a primitive variable used to set the initial state of a JSlider, speedValue, the JSlider's state will then magically change. Java doesn't work this way, and if you want to change JSlider's state, you're going to have to call a method on it that explicitly does this:

slider.setValue(...);

So wherever you wish to change the JSlider's value, you will need to call this method on the appropriate JSlider reference.

You will want to delete your other duplicate question by the way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top