tags:

views:

283

answers:

2

Hi I want horizontal scrollbar which will scroll from left to right automatically after each 1 second.

Thanks Sunil Kumar Sahoo

A: 

How about activating a timer which will set the value of the scroll bar to the value needed?

laginimaineb
Thanks I tried but unable to get it
What have you tried? Post the actual code here (see http://sscce.org) and explain what results you are getting as opposed to what you'd expect.
laginimaineb
+2  A: 

This code sample will provide you with what you need. It creates a scroll pane and scroll the horizontal scrollbar. It also loops from the beginning when it's done.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;

public class Test {

    static String ss = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam lectus ligula, ultricies at varius ut, condimentum eget dui. Cras sapien justo, fermentum vulputate commodo vel, aliquam vitae neque. Mauris cursus bibendum accumsan. Etiam euismod dapibus pellentesque. Phasellus et velit nunc, eget dignissim odio. Duis et nulla eget mauris laoreet venenatis eget a purus. Phasellus congue massa in nulla pellentesque mollis. Proin id lorem ut odio fringilla laoreet et vitae nunc. Nunc non arcu justo, pellentesque posuere nulla. Ut eget sollicitudin libero. Ut at luctus arcu.";
    static int percent = 0;

    public static void main(String[] args) {
     JFrame f = new JFrame();
     JTextArea t = new JTextArea(10,50);
     t.setText(ss);
     final JScrollPane s = new JScrollPane(t);
     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;
       JScrollBar sb = s.getHorizontalScrollBar();
       sb.setValue((int)(sb.getMaximum()*(percent/100.0)));
      }
     });
     time.start();
    }

}
Savvas Dalkitsis
Thanks Savvas,It response really helped me.
how about accepting this as the correct answer?
Savvas Dalkitsis