Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make child auto-resize (child is Pane, parent is VBox) in javafx

Tags:

javafx

pane

vbox

I have 2 questions

1.in a javafx application, I want to put the child(crosshairArea) at the left-top corner of its parent, with 1/2 width and height as well. think I can do that via override the parent function "layoutChildren" (VBox), is there other way to do that? e.g. property binding?

2.initially VBox will occupy the full scene area, how to make(relocate) it to the half-bottom of scene?

public class Crossh extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {        
        VBox root = new VBox(5);
        // root.setPadding(new Insets(20,20,20,20));
        root.setStyle("-fx-border-color:red");

        Pane crosshairArea = new Pane();
        crosshairArea.maxWidthProperty().bind(root.widthProperty());
        crosshairArea.setStyle("-fx-border-color:black");       

        root.getChildren().add(crosshairArea);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("Location Crosshair");
        stage.setWidth(900);
        stage.setHeight(700);        
        stage.show();
    }
}
like image 382
Rui Zhou Avatar asked Jan 12 '23 01:01

Rui Zhou


1 Answers

Set the VBox's last child vgrow property to true.

pane.setVgrow(true);

That will solve the problem.

like image 132
AndreGraveler Avatar answered Feb 04 '23 15:02

AndreGraveler