4

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();
    }
}
Java Man
  • 1,854
  • 3
  • 21
  • 43
Rui Zhou
  • 209
  • 1
  • 3
  • 9

2 Answers2

7

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

pane.setVgrow(true);

That will solve the problem.

AndreGraveler
  • 437
  • 6
  • 12
4

For your first question Have you tried the height property of VBox. For example

root.heightProperty().addListener(new ChangeListener<Number>() {

        @Override
        public void changed(ObservableValue<? extends Number> arg0,
                Number arg1, Number arg2) {
            crosshairArea.setPrefHeight(arg2.doubleValue()/2);

    }
});

For your second question, you will have to put something on top of VBox to occupy the size on the top or you can set the alignment of VBox to Pos.BOTTOM

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • thanks! one more question is why I couldn't bind crosshairArea widthProperty() to its parent? because it is a read only property? and is there any function will be called back by framework once the window initialization complete, e.g. something like MFC onWindowResize(), I suppose I can resize all children there? – Rui Zhou Mar 06 '14 at 10:02
  • I don't think `VBox` is made to support such thing things. Logically, VBox is a `vertical` box and its child must completely fill the width available. Though, I am not sure, and professionals may answer better ! – ItachiUchiha Mar 06 '14 at 11:04
  • For `window resize`, you can bind the `scene` widthProperty and heightProperty ! – ItachiUchiha Mar 06 '14 at 11:04
  • 1
    You can also set the vertical growth with `VBox.setVgrow(child, Priority.ALWAYS);` This worked great for me with a VBox containing a TableView and a Pagination control to create a paginated table of data. The table grows naturally with the height and the pagination control does not, as I set it to Priority.NEVER. No need to add complex, error-prone listeners. – ManoDestra Mar 14 '17 at 17:14