Spawning a new thread to handle while loop

I have two buttons that when I mouse over them I want to scroll my JScrollPane up or down and stop scrolling when I exit the button. The following obviously doesn't work because the while loop never allows scrolling to be changed to false when the mouse exits.
Pretty sure I'm supposed to be spawning a new thread for the while loop. Can anyone post the code that I would need to implement this?

      public void mouseEntered(MouseEvent e) {
            int yOffset = -10; // up by default
            scrolling = true;

            if (e.getSource() == upArrow)
                  upArrow.toggleImage();
            else
            {
                  downArrow.toggleImage();
                  yOffset = 10;
            }
                  while (scrolling) {
                  Point p = vp.getViewPosition();
                  p.translate(0, yOffset);
                  vp.setViewPosition(p);
            }
      }
      public void mouseExited(MouseEvent e) {
            scrolling = false;
            if (e.getSource() == upArrow)
                  upArrow.toggleImage();
            else
                  downArrow.toggleImage();
      }

LVL 1
darkpegasus5Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
use a Swing timer

Declare your timer:

  int delay = 500; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
                  Point p = vp.getViewPosition();
                  p.translate(0, yOffset);
                  vp.setViewPosition(p);      }
  };
  timer = new Timer(delay, taskPerformer);

to start:

  timer.start();


to stop:

timer.stop();
0
 
UrosVidojevicCommented:
Another possible solution:
Try inserting this inside your class:

ScrollThread t = new ScrollThread(vp);

    class ScrollThread extends Thread {
        private static final int INTERVAL = 100;
        // you can adjust interval
       
        private boolean scrolling;
        private ViewPort vp;
        private int yOffset;
       
        ScrollThread(ViewPort vp) {
            this.vp = vp;
            start();
        }
       
        synchronized void startScrollingUP() {
            yOffset = -10;
            scrolling = true;
            notifyAll();
        }

        synchronized void startScrollingDOWN() {
            yOffset = 10;
            scrolling = true;
            notifyAll();
        }
       
        synchronized void stopScrolling() {
            scrolling = false;
        }
         
        void end() {
            interrupt();
        }
       
        public void run() {
            try {
                while (!interrupted()) {
                    if (!scrolling )
                        synchronized (this) {
                            wait();
                        }
                   
                    Point p = vp.getViewPosition();
                    p.translate(0, yOffset);
                    vp.setViewPosition(p);
                 
                    sleep(INTERVAL);
                }
            } catch (InterruptedException ie) {}
        }
    }

public void mouseEntered(MouseEvent e) {
            if (e.getSource() == upArrow) {
                  upArrow.toggleImage();
                  t.startScrollingUP();
            }      
            else
            {
                  downArrow.toggleImage();
                  t.startScrollingDOWN();
            }
}

public void mouseExited(MouseEvent e) {
            t.stopScrolling();
}

At the end of program call t.end();
You can adjust interval if you want to higher or lower values.
0
 
darkpegasus5Author Commented:
That was perfect, thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.