tags:

views:

209

answers:

3

How would I create a horizontal slider which will slide from left to right automatically after each 1 second.

+1  A: 

You already got your answer to this question ( you've prevously ask ): http://stackoverflow.com/questions/1351055/how-to-create-a-horizontal-scrollbar-which-will-scrolls-automatically-after-some

Nettogrof
Hi that answer is for horizontal scrolbar now my problem is with slider
Well, the concept is the same. You use a Timer. Given that in your previous questions you have yet to "accept" an answer I think thats about a detailed as anyone will be for this question.
camickr
+1  A: 
static final int FPS_MIN = 0;
static final int FPS_MAX = 30;
static final int FPS_INIT = 15;    

JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL,
                                      FPS_MIN, FPS_MAX, FPS_INIT);

framesPerSecond.setMajorTickSpacing(10);
framesPerSecond.setMinorTickSpacing(1);
framesPerSecond.setPaintTicks(true);
framesPerSecond.setPaintLabels(true);

http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html

adatapost
-1 The slider doesn't move automatically every second. There is no functionality built into the slider to do that.
camickr
+1  A: 

/* * Copyright © 2009. Artificial Machines Pvt. Ltd. India. * All rights reserved. * This file is a property of Artificial Machines Pvt. Ltd. * It is illegal to modify, copy or use any part of this file, for * any purpose outside of Artificial Machines Pvt. Ltd. products. */

package trypls;

/** * * @author sunil.s */ import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

import javax.swing.JFrame; import javax.swing.JSlider; import javax.swing.Timer;

public class Test {

static int percent = 0;

public static void main(String[] args) {
    JFrame f = new JFrame();
    final JSlider s = new JSlider();
    f.getContentPane().add(s);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
    Timer time = new Timer(100, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                    percent++;
                    if (percent>100)
                            percent = 0;

// s.setMajorTickSpacing(10); //s.setMinorTickSpacing(1); //s.setPaintTicks(true); //s.setPaintLabels(true);

// JScrollBar sb = s.getHorizontalScrollBar(); s.setValue((int)(s.getMaximum()*(percent/100.0))); s.setAutoscrolls(true); } }); time.start(); }

}