Question

I am creating a simple "Play/rewind" interface for a simulation, with a JSlider that functions as both a Progress Bar and a Slider that you can move the simulation to any point in time within the simulation bounds. The sliding to the point in time works perfectly, however I cannot get the actual slider to move while the simulation is "Playing". Link to the GUI is shown below for added assistance:

http://i.stack.imgur.com/7rAWf.png

A simplified version of what I have going on

Main object has 2 objects, the GUI, and the Controller. gui: Creates the slider/buttons and a Controller. Slider's and Button's change/action listeners are set to the Controller

Controller: Various buttons that do the play/rewind functions. This is handled through the use of a swing Timer that repaints the simulation every "Frame". With the slider, the "CurrentFrame" is changed as you drag the slider across, and the repaint function is called. What it is NOT doing is sliding itself while the timer is running. Code for the stateChanged is below:

    public void stateChanged(ChangeEvent z) 
    {
        JSlider source = (JSlider)z.getSource();

        //If statement that checks if the state of the slider is done changing when the change listener is evoked
        if(!source.getValueIsAdjusting())
        {
            CurrentFrame = (int)source.getValue();
            colladaRoot.setPosition(framePos[CurrentFrame]);
            wwd.repaint();
        }
        // My attempt at checking if the timer is running, and changing the value of the slider
        // while it is.
        while (timer.isRunning())
            source.setValue(CurrentFrame);

    }

In case its requested, this is also the slider creating in the GUI class:

            JSlider framecontroller = new JSlider(0, LastFrame, CurrentFrame);
            framecontroller.addChangeListener(this.controller); 
            framecontroller.setMajorTickSpacing(200);
            framecontroller.setMinorTickSpacing(1);
            framecontroller.setPaintTicks(true);
            framecontroller.setPaintLabels(true);
Was it helpful?

Solution

You don't want to check the timer state from the ChangeEventListener, that only gets fired when you move the slider. You need to add an ActionListener to the Timer and update the slider from there

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