This discussion is archived
7 Replies Latest reply: Oct 8, 2012 12:42 AM by 963923 RSS

How to control ScrollPane on click event of button

963923 Newbie
Currently Being Moderated
Hi. i want to control scrollpane on mouse clicked event.I have added 2 button one at left side and other at right side.And at center i have added HBox containg images.Now i want to slide images on click event of button. So How can i control the scrollpane so that images as well as scrollpane's thumb also slides.
  • 1. Re: How to control ScrollPane on click event of button
    David Grieve Pro
    Currently Being Moderated
    From the button's onAction, call one of the ScrollBar's methods - increment, decrement, setValue or adjustValue.
  • 2. Re: How to control ScrollPane on click event of button
    shakir.gusaroff Expert
    Currently Being Moderated
    Hi. There is an example for the display shelf:
    http://download.oracle.com/otndocs/products/javafx/2.2/samples/Ensemble/index.html#SAMPLES/Graphics/Display%20Shelf
  • 3. Re: How to control ScrollPane on click event of button
    963923 Newbie
    Currently Being Moderated
    thanks david
  • 4. Re: How to control ScrollPane on click event of button
    963923 Newbie
    Currently Being Moderated
    hello david, see this code the ScrollPane thumb is not movable even if i click on either scrollpane's < > buttons. If u runs this under javafx 2.2 thumb is not movable, but it works under javafx 2.0.3

    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Priority;
    import javafx.stage.Stage;

    public class MainDemo extends Application {

    final ScrollPane sp = new ScrollPane();
    final Image[] images = new Image[5];
    final ImageView[] pics = new ImageView[5];
    final HBox vb = new HBox();
    final Label fileName = new Label();
    final String[] imageNames = new String[]{"http://images3.pics-united.com/fotos/big/1233/123309163953939.jpg", "http://images3.pics-united.com/fotos/big/1233/123309163953939.jpg",
    "http://images3.pics-united.com/fotos/big/1233/123309163953939.jpg", "http://images3.pics-united.com/fotos/big/1233/123309163953939.jpg", "http://images3.pics-united.com/fotos/big/1233/123309163953939.jpg"};

    @Override
    public void start(Stage stage) {
    BorderPane border = new BorderPane();
    //border.setTop(null);


    HBox box = new HBox();
    // Scene scene = new Scene(box, 180, 180);
    // stage.setScene(scene);
    stage.setTitle("Scroll Pane");
    box.getChildren().addAll(sp, fileName);
    HBox.setHgrow(sp, Priority.ALWAYS);

    fileName.setLayoutX(30);
    fileName.setLayoutY(160);

    for (int i = 0; i < 5; i++) {
    images[i] = new Image(imageNames);
    pics[i] = new ImageView(images[i]);
    pics[i].setFitWidth(100);
    pics[i].setPreserveRatio(true);
    vb.getChildren().add(pics[i]);
    }

    sp.setHmax(440);
    sp.setPrefSize(115, 150);
    sp.setContent(vb);

    sp.vvalueProperty().addListener(new ChangeListener<Number>() {

    @Override
    public void changed(ObservableValue<? extends Number> ov,
    Number old_val, Number new_val) {
    fileName.setText(imageNames[(new_val.intValue() - 1) / 100]);
    }
    });

    BorderPane bp = new BorderPane();
    bp.setPadding(new Insets(10, 20, 10, 20));

    //Adding buttons to BorderPane
    Button btnTop = new Button("Top");
    //     bp.setTop(btnTop);

    Button btnLeft = new Button("Left");
    bp.setLeft(btnLeft);

    Button btnCenter = new Button("Center");
    bp.setCenter(box);

    Button btnRight = new Button("Right");
    bp.setRight(btnRight);

    Button btnBottom = new Button("Bottom");
    //     bp.setBottom(btnBottom);

    //Adding BorderPane to the scene
    Scene scene = new Scene(bp, 300, 100);
    stage.setScene(scene);
    stage.show();
    }

    public static void main(String[] args) {
    launch(args);
    }
    }
  • 5. Re: How to control ScrollPane on click event of button
    David Grieve Pro
    Currently Being Moderated
    I think what is messing you up calling setHmax on the ScrollPane.
  • 6. Re: How to control ScrollPane on click event of button
    shakir.gusaroff Expert
    Currently Being Moderated
    Button btnLeft = new Button("Left");
            btnLeft.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    
                    sp.hvalueProperty().set(sp.getHvalue()- 5 );
                    
                }
            });
    
    
            Button btnRight = new Button("Right");
             btnRight.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    
                    sp.hvalueProperty().set(sp.getHvalue()+ 5 );
                    
                }
            });
    Here is your modified code:
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Priority;
    import javafx.stage.Stage;
    
    public class MainDemo extends Application {
    
        final ScrollPane sp = new ScrollPane();
        final Image[] images = new Image[5];
        final ImageView[] pics = new ImageView[5];
        final HBox vb = new HBox();
        final Label fileName = new Label();
        final String[] imageNames = new String[]{"http://images3.pics-united.com/fotos/big/1233/123309163953939.jpg", "http://images3.pics-united.com/fotos/big/1233/123309163953939.jpg",
            "http://images3.pics-united.com/fotos/big/1233/123309163953939.jpg", "http://images3.pics-united.com/fotos/big/1233/123309163953939.jpg", "http://images3.pics-united.com/fotos/big/1233/123309163953939.jpg"};
    
        @Override
        public void start(Stage stage) {
            BorderPane border = new BorderPane();
    //border.setTop(null);
    
           HBox box = new HBox();
    // Scene scene = new Scene(box, 180, 180);
    // stage.setScene(scene);
            stage.setTitle("Scroll Pane");
            box.getChildren().addAll(sp, fileName);
            HBox.setHgrow(sp, Priority.ALWAYS);
    
            fileName.setLayoutX(30);
            fileName.setLayoutY(160);
    
            for (int i = 0; i < 5; i++) {
                images[i] = new Image(imageNames);
    pics[i]= new ImageView(images[i]);
    pics[i].setFitWidth(100);
    pics[i].setPreserveRatio(true);
    vb.getChildren().add(pics[i]);
    }

    sp.setHmax(440);
    sp.setPrefSize(115, 150);
    sp.setContent(vb);

    /*
    sp.vvalueProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> ov,
    Number old_val, Number new_val) {
    System.out.println(new_val.intValue());
    fileName.setText(imageNames[(new_val.intValue() - 1) / 100]);
    }
    });
    */
    /*
    sp.hvalueProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> ov,
    Number old_val, Number new_val) {
    System.out.println(new_val.intValue());
    fileName.setText(imageNames[(new_val.intValue() - 1)]);
    }
    });
    */

    BorderPane bp = new BorderPane();
    bp.setPadding(new Insets(10, 20, 10, 20));

    //Adding buttons to BorderPane
    Button btnTop = new Button("Top");
    // bp.setTop(btnTop);




    Button btnLeft = new Button("Left");
    btnLeft.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {

    sp.hvalueProperty().set(sp.getHvalue()- 5 );

    }
    });

    bp.setLeft(btnLeft);

    Button btnCenter = new Button("Center");
    bp.setCenter(box);

    Button btnRight = new Button("Right");
    btnRight.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {

    sp.hvalueProperty().set(sp.getHvalue()+ 5 );

    }
    });

    bp.setRight(btnRight);

    Button btnBottom = new Button("Bottom");
    // bp.setBottom(btnBottom);

    //Adding BorderPane to the scene
    Scene scene = new Scene(bp, 300, 100);
    stage.setScene(scene);
    stage.show();
    }

    public static void main(String[] args) {
    launch(args);
    }
    }
  • 7. Re: How to control ScrollPane on click event of button
    963923 Newbie
    Currently Being Moderated
    yes that is correct code on buttons click event,but i am talking about scrollpane's buttons not that btnLeft & btnRight. See ScrollPane consist of 2 btns 1 at left and other at right and 1 thumb, but that default 2 buttons (of scrollapane) are not working and also thumb is not movable when i click on scrollpane. I want to set blockincrement on scrollpane so that images can move in either site. See i could have use ScrollBar instead of ScrollPane,but i want to use ScrollPane. And also how to increase thumb width so that we can scroll faster.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points