tags:

views:

210

answers:

1

I'm trying to make a scroll button using a ImageView that should scroll some images while it's pressed. I tried to do it with the primaryButtonDown property of the MouseEvent, but it freezes the application:

ivbutton.onMousePressed = function (e) {
     while (e.primaryButtonDown){
       //Scroll Stuff
     }

Do you know a way to create this kind of button?

Thanks.

+2  A: 

Could you link the scrolling to a Timeline animation? Then the button could control the start and stop of the timeline.

For example, this is similar but it controls the rotation of a square (maybe you could use a TranslateTransition):

import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.animation.transition.RotateTransition;
import javafx.animation.Timeline;
import javafx.scene.control.Button;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.Interpolator;

def r = Rectangle {
    x: 80 y: 80
    height: 100 width:  100
    arcHeight: 50 arcWidth: 50
    fill: Color.VIOLET
}

def rotTransition = RotateTransition {
    duration: 1s
    node: r
    byAngle: 180
    repeatCount: Timeline.INDEFINITE
    interpolator: Interpolator.LINEAR
}

def b = Button {
    text: "Click and hold"
    onMousePressed: function (e) {
      rotTransition.play();
     };
     onMouseReleased: function (e) {
      rotTransition.stop();
     };
};
Stage {
    title : "Rotation"
    scene: Scene {
     width: 250
     height: 250
     content: [r,b]
    }
}
Matthew Hegarty
Thanks, Mash. Works great.
Averroes