views:

226

answers:

3

My apologies for posting tons of questions as of late.

I'm trying to get a JSlider to update its position based on a constantly updating variable.

The setValue(n) method doesn't seem to work. Is there any alternative? We're using this as a time marker for a music player.

+1  A: 

Provided that you've correctly configured a minimum and maximum value, setValue() will work. The JSlider does need an opportunity to redraw, so make sure you're not tying up the event dispatch thread. If your code is responding to any UI event, you're on the dispatch thread and should return as soon as possible.

Perform any expensive calculations on a background thread. There are a plethora of excellent tutorials for this on the web.

Matthew
A: 

The setValue method definitely does work, so I would check that the value that you're passing in to setValue is indeed within the minimum and maximum value that you've configured the slider with (ie check that you're not passing a percentage value in instead of an integer).

A second thing to watch out for: you should only be calling setValue on the event dispatching thread. Of course you can calculate the value that is going to be passed to setValue outside of the even dispatching thread, but any update to a value in a Swing component should be done in the even dispatching thread (ie using SwingUtilities.invokeLater)

gab
A: 

I am facing the same issue and wonder why setValue is not working on the code i was working on. Here is a short example that works.

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Write a description of class SSCCESlider here.
 * 
 * @author (your name)
 * @version (a version number or a date)
 */
public class SSCCESlider {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Example");
        frame.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        final JLabel label = new JLabel();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        frame.add(label, c);

        JSlider boxSizeSlider = new JSlider(JSlider.HORIZONTAL, 10, 45, 40);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        boxSizeSlider.setMajorTickSpacing(10);
        boxSizeSlider.setPaintTicks(true);
        boxSizeSlider.setPaintLabels(true);
        boxSizeSlider.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                final JSlider source = (JSlider) e.getSource();
                if (!source.getValueIsAdjusting()) {
                    label.setText(source.getValue() + "");
                    if (source.getValue() < 30) {
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                source.setValue(30);
                            }
                        });

                        // wouldnt work
                        // source.setValue(30);

                        // wouldnt work
                        // try {
                        // SwingUtilities.invokeAndWait(new Runnable() {
                        // public void run() {
                        // source.setValue(30);
                        // }
                        // });
                        // } catch (InterruptedException e1) {
                        // // TODO Auto-generated catch block
                        // e1.printStackTrace();
                        // } catch (InvocationTargetException e1) {
                        // // TODO Auto-generated catch block
                        // e1.printStackTrace();
                        // }

                    }
                }

            }
        });
        boxSizeSlider.getModel().setValue(30);
        frame.add(boxSizeSlider, c);

        label.setText(boxSizeSlider.getValue() + "");

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
zeroin23