Jeevanantham
Jeevanantham

Reputation: 1022

Javafx - How to set drag range for components added in a pane

i have tried a below sample, in which left area of border Pane will have list of components and center of the border pane will act as a canvas area and here i have added a rectangle on run time as children to a Pane which is set to Center portion of BorderPane. But when drag the rectangle it moving outof the area allocated for the center, so how could i make this drag around only inside the Center Pane.

enter image description here

@Override
public void start(Stage stage) throws Exception {
    stage.setTitle("BPM");
    BorderPane border = new BorderPane();

    Pane canvas = new Pane();
    canvas.setStyle("-fx-background-color: #F0F0F0;");
    border.setLeft(compList());
    border.setCenter(canvas);
    //
    Anchor start = new Anchor(null, "Start", Color.PALEGREEN, new SimpleDoubleProperty(170), new SimpleDoubleProperty(170));

    final Rect rect=new Rect(100, 70,new SimpleDoubleProperty(10), new SimpleDoubleProperty(100));        
    rect.setX(100);
    rect.setY(100);

    canvas.getChildren().add(rect);
    canvas.getChildren().add(start);



    Scene scene = new Scene(border, 800, 600);
    stage.setScene(scene);
    stage.show();

}

Upvotes: 0

Views: 1306

Answers (1)

Muhammad Rosli
Muhammad Rosli

Reputation: 303

Actually, by default, the Pane class does not ensure that all its children are clipping hence there are possibility that the children might go out of the boundary of the Pane. To ensure that all children (in your case, the rectangle) are dragged within specify boundary, you have to manually check the boundary as you dragging the children. Below are example of my implementation:

@Override
public void start(Stage stage){
    stage.setTitle("BPM");
    BorderPane mainPanel = new BorderPane();

    VBox nameList = new VBox();
    nameList.getChildren().add(new Label("Data"));
    nameList.setPrefWidth(150);

    Pane canvas = new Pane();
    canvas.setStyle("-fx-background-color: #ffe3c3;");
    canvas.setPrefSize(400,300);

    Circle anchor = new Circle(10);

    double rectWidth = 50, rectHeight = 50;
    Rectangle rect = new Rectangle(50,50);
    rect.setX(100);
    rect.setY(100);

    canvas.getChildren().addAll(rect, anchor);

    // set the clip boundary
    Rectangle bound = new Rectangle(400,300);
    canvas.setClip(bound);

    rect.setOnMouseDragged(event -> {

        Point2D currentPointer = new Point2D(event.getX(), event.getY());

        if(bound.getBoundsInLocal().contains(currentPointer)){

            if(currentPointer.getX() > 0 &&
                    (currentPointer.getX() + rectWidth) < bound.getWidth()){
                rect.setX(currentPointer.getX());
            }
            if(currentPointer.getY() > 0 &&
                    (currentPointer.getY() + rectHeight) < bound.getHeight()){
                rect.setY(currentPointer.getY());
            }
        }
    });

    mainPanel.setLeft(nameList);
    mainPanel.setCenter(canvas);
    Scene scene = new Scene(mainPanel, 800, 600);
    stage.setScene(scene);
    stage.show();
}

Upvotes: 1

Related Questions