Hello, this is my first post.
I'd like to add zoom in/out function to my ScrollPane, and make shortcut (CTRL+MOUSE SCROLL UP/DOWN) for it. ( like Inkscape )
I made the following code (
Canvas ) to achieve this, but have some problems.
*1. How can I prevent ScrollBar's thumb to move when pressing CTRL?*
When ScrollBar is visible and ScrollBar's thumb (or scroller, knob) has room to move, CTRL+MOUSE SCROLL UP/DOWN doesn't work because the thumb moves instead of zooming in/out.
When the thumb moves up/down to bar's end, then zoom in/out finally works fine.
*2. ScrollEvent handler is sometimes not called in ScrollPane*
When ScrollBarPolicy is not ScrollBarPolicy.ALWAYS and Scrollbar is not visible,
SOMETIMES CTRL+SCROLL UP/DOWN operation dosen't work.
(ScrollEvent handler is not called. For other panes, ScrollEvent works fine. Please check
MouseScrollEventTest below)
Thank you in advance.
Canvas
public class Canvas extends Application {
DoubleProperty zoom = new SimpleDoubleProperty(1.0);
final double MAX_ZOOM = 3.0;
final double MIN_ZOOM = 0.1;
@Override
public void start(Stage stage) throws Exception {
Rectangle rectangle = RectangleBuilder.create()
.width(1000).height(1000)
.fill( LinearGradientBuilder.create()
.stops(new Stop(0, Color.BLUE), new Stop(1, Color.RED))
.build()
).build();
ScrollPane scrollPane = ScrollPaneBuilder.create()
.content(
GroupBuilder.create()
.children(rectangle)
.build())
// .hbarPolicy(ScrollBarPolicy.ALWAYS)
// .vbarPolicy(ScrollBarPolicy.ALWAYS)
.build();
rectangle.scaleXProperty().bind(zoom);
rectangle.scaleYProperty().bind(zoom);
scrollPane.setOnScroll(new EventHandler<ScrollEvent>(){
public void handle(ScrollEvent event) {
if( !event.isControlDown() ) return ;
double zoomValue;
if ( event.getDeltaY() > 0 ) {
zoomValue = zoom.get() + 0.1;
} else {
zoomValue = zoom.get() - 0.1;
}
if( zoomValue > MAX_ZOOM || zoomValue < MIN_ZOOM ) return ;
zoom.set(zoomValue);
}
});
Scene scene = SceneBuilder.create()
.width(500).height(500)
.root(scrollPane)
.build();
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Mouse Scroll Event Test
public class MouseScrollEventTest extends Application {
BorderPane bPane;
ScrollPane sPane;
public void start(Stage stage) throws Exception {
EventHandler<ScrollEvent> handler = new EventHandler<ScrollEvent>() {
public void handle(ScrollEvent event) {
System.out.println(event);
}
};
bPane = new BorderPane();
bPane.setPrefSize(100, 100);
sPane = new ScrollPane();
sPane.setPrefSize(100, 100);
// sPane.setContent( new Rectangle(50,50) );
// This works fine.
bPane.setOnScroll(handler);
// Sometimes, this is not called.
sPane.setOnScroll(handler);
HBox root = new HBox();
root.getChildren().addAll(bPane, sPane);
Scene scene = new Scene(root);
// scene.setOnScroll(handler);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
--- My Environment
Windows Vista
JavaFX 2.0 SDK
---
Edited by: 932474 on 2012/05/07 4:31
Edited by: 932474 on 2012/05/07 4:47