By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

Automatic scroll of JSlider

P: 1
Hi, I was just wondering would anybody be able to show us how to use a button, that when pressed, automatically scrolls a JSlider across say from 0 - 100 at a certain speed or whatever.

If anyone can help, I'd really appreciate it
Nov 20 '06 #1
Share this Question
Share on Google+
2 Replies


sivadhas2006
100+
P: 142
Hi,

This may satisfy your need...

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.BorderLayout;
  3. import java.awt.Container;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JSlider;
  9. public class TestFrame extends JFrame
  10. {
  11. private static final long serialVersionUID = 1L;
  12. JButton                 m_btnMoveSlider = null;
  13. JSlider                 m_objSlider     = null;
  14. public class TestThread extends Thread
  15. {
  16.         public void run()
  17.         {
  18.             super.run();
  19.             int nValue = m_objSlider.getValue();
  20.             int nMaxValue = m_objSlider.getMaximum();
  21.             do
  22.             {
  23.              nValue = nValue + 5;
  24.              try
  25.              {
  26.                  Thread.sleep(100);
  27.              }
  28.              catch (InterruptedException e)
  29.              {
  30.                  e.printStackTrace();
  31.              }
  32.              m_objSlider.setValue(nValue);
  33.             }
  34.             while (nValue < nMaxValue);
  35.             m_btnMoveSlider.setEnabled(true);
  36.             m_btnMoveSlider.requestFocus();
  37.         }
  38. };
  39. TestFrame()
  40. {
  41.         m_btnMoveSlider = new JButton("Move Slider");
  42.         m_objSlider = new JSlider(0, 100, 0);
  43.         m_btnMoveSlider.addActionListener(new ActionListener() {
  44.             public void actionPerformed(ActionEvent a_e)
  45.             {
  46.              m_objSlider.setValue(0);
  47.              m_btnMoveSlider.setEnabled(false);
  48.              TestThread objThread = new TestThread();
  49.              objThread.start();
  50.             }
  51.         });
  52.         Container con = getContentPane();
  53.         con.setLayout(new BorderLayout());
  54.         con.add(m_btnMoveSlider, BorderLayout.NORTH);
  55.         con.add(m_objSlider, BorderLayout.CENTER);
  56.         setSize(500, 100);
  57.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58. }
  59. public static void main(String[] args)
  60. {
  61.         TestFrame objJFrame = new TestFrame();
  62.         objJFrame.show();
  63. }
  64. };
  65.  
  66.  
Regards,
M.Sivadhas.
Nov 20 '06 #2

P: 1
@sivadhas2006
your answer was really helpful to me too. Thanks
Dec 12 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.