sunil
sunil

Reputation:

How to create a horizontal slider

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

Upvotes: 0

Views: 2645

Answers (2)

sunil
sunil

Reputation:

/*
 * 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();
    }

}

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94645

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

Upvotes: 2

Related Questions