Skip to content

Instantly share code, notes, and snippets.

@bugabinga
Created January 19, 2016 09:10
Show Gist options
  • Save bugabinga/00e675029df3aa771f76 to your computer and use it in GitHub Desktop.
Save bugabinga/00e675029df3aa771f76 to your computer and use it in GitHub Desktop.
Example of forcing alignment in a FlowPane.
package com.isp.stackoverflow;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
/**
* Example of forcing alignment in a FlowPane.
*
* @author okr
* @date 19.01.2016
*
*/
public class FlowSpacer extends Application
{
@Override
public void start( final Stage primaryStage )
{
final Button btn1 = new Button( "One" );
final Button btn2 = new Button( "Two" );
final Button btn3 = new Button( "Three" );
final Button btn4 = new Button( "Four" );
final Label label = new Label( "My Label" );
final FlowPane root = new FlowPane( btn1, btn2, label, btn3, btn4 );
final Scene scene = new Scene( root );
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 ) );
} );
primaryStage.setScene( scene );
primaryStage.show();
}
/**
* @param args ignored.
*/
public static void main( final String[] args )
{
launch( args );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment