user2022068
user2022068

Reputation:

JavaFx: liquid and fixed gridpane column widths

I have gridpane with 4 columns (fixed1,liquid,fixed2,liquid). The every liquid column must have the width= (gridpane width-fixed1-fixed2)/2. By other words 50% of liquid width. Important: all width=liquid width+fixed width. In order to set column constraint I've found the following code:

GridPane gridpane = new GridPane();
ColumnConstraints col1 = new ColumnConstraints();
col1.setPercentWidth(25);
gridpane.getColumnConstraints().addAll(col1);

However, this code sets percent of not liquid width but of all width. How to do it?

Upvotes: 3

Views: 11184

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49215

Try this;

@Override
public void start( final Stage primaryStage )
{

    ColumnConstraints col1 = new ColumnConstraints();
    col1.setHgrow( Priority.ALWAYS );

    ColumnConstraints col2 = new ColumnConstraints();
    col2.setHgrow( Priority.ALWAYS );

    GridPane gridPane = new GridPane();
    gridPane.setGridLinesVisible( true );
    gridPane.getColumnConstraints().addAll( new ColumnConstraints( 60 ), col1, new ColumnConstraints( 100 ), col2 );

    gridPane.addColumn( 0, new Button( "col 1" ) );
    gridPane.addColumn( 1, new Button( "col 2" ) );
    gridPane.addColumn( 2, new Button( "col 3" ) );
    gridPane.addColumn( 3, new Button( "col 4" ) );

    final Scene scene = new Scene( new VBox( gridPane ), 400, 300 );
    primaryStage.setScene( scene );
    primaryStage.show();
}

Test the occupied widths by resizing the window. The free space is shared equally between the columns whose has a Priority.ALWAYS grow. There are 2 of them, so 50% for each. As a result it is distributed as:

col-1: fixed 60px
col-2: floated 50%
col-3: fixed 100px
col-4: floated 50%

Upvotes: 6

Related Questions