This discussion is archived
2 Replies Latest reply: Jan 10, 2013 7:57 AM by 982987 RSS

Proportional anchors with AnchorPane

982987 Newbie
Currently Being Moderated
I am sort of new to JavaFx so I could have missed this all together. I want the ability to provide anchors that are a percentage of the container size. For instance, a Button that is always 25% of the Pane width from the left and 10px from the top. SWT offers this functionality (along with the ability to anchor to other controls) but these abilities are missing from JavaFx. The post {message:id=10697683} seems to indicate that MigLayout might be a possible solution to this.

1) Why were these features not included? I'm just wondering is there something about this problem that might prove incompatible with the JavaFx architecture or layout engine such that it would prove to be a massive hack to write a special pane myself?
2) Does anyone know if MigLayout works well (or at all) for these use cases?

Edited by: 979984 on Jan 4, 2013 9:36 AM
  • 1. Re: Proportional anchors with AnchorPane
    twasyl Newbie
    Currently Being Moderated
    WHat you can do is using bindings. JavaFX relies a lot that (and it is very powerful). In your case it could be something that look like:
    AnchorPane container = new AnchorPane();
    Button button = new Button("Test button");
    
    // Says that the button is 25% pixel from the left
    button.layoutXProperty().bind(container.widthProperty().multiply(0.25d));
    
    // 47% from the top
    button.layoutYProperty().bind(container.heightProperty().multiply(0.47d));
    
    container.getChildren().add(button);
    More information about properties and binding:
    - http://thierrywasyl.wordpress.com/2012/07/29/properties-and-bindings-in-javafx/
    - http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm

    Hope it helps
  • 2. Re: Proportional anchors with AnchorPane
    982987 Newbie
    Currently Being Moderated
    Thanks for the answer. I had not really considered bindings in a regular Pane to replace a custom Pane implementation but that seems logical. After grabbing the source for the AnchorPane I just extended the library version, and overrode the layoutChildren() method with the opensource version and replaced where it was grabbing the anchors. This worked really well for a two line code change proof of concept but the devil will be in the details (may be assuming too much) so it's always nice to have a plan B.

Legend

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