java - JavaFX GridPane resizing of pane children -


this may duplicate question, haven't found solution suits needs.

inspired jewelsea's colorchoosersample, half-way through implementation realised manual size can set on controls, , not on panes. these panes should smart enough resize automatically depending on parent.

is there way gridpanes children grab vertical , horizontal space @ same time? vbox , hbox combined. bet solution involves anchorpane.

keep in children panes , not controls.


sscce buttons (copy - paste - run - resize window)

import javafx.application.application; import javafx.beans.property.doubleproperty; import javafx.beans.property.simpledoubleproperty; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.geometry.bounds; import javafx.geometry.insets; import javafx.scene.node; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.control; import javafx.scene.layout.gridpane; import javafx.stage.stage;  /**  *   * @author ggrec  *  */ public class dashboardfxgrid extends application {      // ==================== 1. static fields ========================      private final static double golden_ratio = 1.618;      private final static double min_tile_size = 5;     private final static double max_tile_size = double.max_value;       // ====================== 2. instance fields =============================      private doubleproperty preftilesize = new simpledoubleproperty(min_tile_size);      private double ncolumns;     private double nrows;      private gridpane gridpane;       // ==================== 3. static methods ====================      public static void main(final string[] args)     {         application.launch(args);     }       // ==================== 4. constructors ====================      @override     public void start(final stage primarystage) throws exception     {         gridpane = new gridpane();          gridpane.setpadding(new insets(20));         gridpane.sethgap(10);         gridpane.setvgap(10);          ncolumns = math.floor(math.sqrt(dummybuttons().length) * 2 / golden_ratio);         nrows    = math.ceil(dummybuttons().length / ncolumns);          createcontents();          addresizelisteners();          primarystage.setscene(new scene(gridpane));         primarystage.show();     }       // ==================== 5. creators ====================      private void createcontents()     {         int = 0;          (final button button : dummybuttons())         {             gridpane.setrowindex(button,       / (int) ncolumns);             gridpane.setcolumnindex(button, % (int) ncolumns);              button.setminsize(min_tile_size, min_tile_size);             button.setmaxsize(max_tile_size, max_tile_size);              gridpane.getchildren().add(button);              i++;         }     }      private void addresizelisteners()     {         gridpane.layoutboundsproperty().addlistener(new changelistener<bounds>() {              @override public void changed(final observablevalue<? extends bounds> observablevalue, final bounds oldbounds, final bounds newbounds)             {                 preftilesize.set(math.max(min_tile_size, math.min(newbounds.getwidth() / ncolumns, newbounds.getheight() / nrows)));                  (final node child : gridpane.getchildrenunmodifiable())                 {                     final control tile = (control) child;                     tile.setprefsize(preftilesize.get(), preftilesize.get());                 }             }         });     }       // ==================== 15. other ====================      private static final button[] dummybuttons()     {         final button[] buttons = new button[5];          for(int = 0; < buttons.length; i++)         {             buttons[i] = new button(string.valueof(i));         }          return buttons;     }  } 

is there way gridpanes children grab vertical , horizontal space @ same time?

try use rowconstraints , columnconstraints of gridpane:

for (final button button : dummybuttons()) {     gridpane.setrowindex(button, / (int) ncolumns);     gridpane.setcolumnindex(button, % (int) ncolumns);      button.setminsize(min_tile_size, min_tile_size);     button.setmaxsize(max_tile_size, max_tile_size);      gridpane.getchildren().add(button);      i++; }  (int j = 0; j < ncolumns; j++) {     columnconstraints cc = new columnconstraints();     cc.sethgrow(priority.always);     gridpane.getcolumnconstraints().add(cc); }  (int j = 0; j < nrows; j++) {     rowconstraints rc = new rowconstraints();     rc.setvgrow(priority.always);     gridpane.getrowconstraints().add(rc); } 

and without calling addresizelisteners().


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -