Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Spawning a new thread to handle while loop

Posted on 2007-10-07
3
Medium Priority
?
260 Views
Last Modified: 2010-03-30
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();
      }

0
Comment
Question by:darkpegasus5
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 20029567
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
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20029595
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
 
LVL 1

Author Comment

by:darkpegasus5
ID: 20029735
That was perfect, thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question