javajavafx-8panes

FlowPane and Positioning


Is it possible to use a flowpane in the following way? Have two components (img/label) aligned to the left and then multiple buttons on the right. Example:

    +----------------------------------------------------------------+
    | +------+ +----------+                          +-----+ +-----+ |
    | |  Img | | Text...  |                          | btn | | btn | |
    | +------+ +----------+                          +-----+ +-----+ |
    +----------------------------------------------------------------+

I am adding the buttons for design / ease of use but am running into a brick wall. I would prefer not to have to change the 'holding panel'.

If not can it be simulated in css (float?)

Thanks


Solution

  • A FlowPanecan set margins. Here is an example that shows how to calculate the width of the margin so that the buttons are right aligned.

    scene.widthProperty().addListener( ( observable, oldWidth, newWidth ) ->
    {
      final double spacerMargin = newWidth.doubleValue()
          - scene.getRoot().getChildrenUnmodifiable().stream().mapToDouble( node -> node.getLayoutBounds().getWidth() ).sum();
      FlowPane.clearConstraints( btn3 );
      FlowPane.setMargin( btn3, new Insets( 0, 0, 0, spacerMargin ) );
    } );
    

    You basically subtract all the widths of the children of the FlowPane from the width of your scene.