There is a form, there is a GridPane in it. There are elements in the GridPane, suppose Rectangle (squares), and the form can stretch along with the GridPane. elements that do not fit on the right, moved down
Add dynamically when you press the Rectangle button without specifying the cells (in the free space below and above, shifting the remaining elements)
- oneVBox and HBox - jessez may be suitable for such purposes July
|
2 answers
Well, regarding the first point, here you have a sample code with a listener responsible for such changes:
public class ResizableGridPane extends Application { private static final Double rectaWidth = 100d, rectaHeigth = 50d; public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) throws Exception { GridPane grid = new GridPane(); AnchorPane root = new AnchorPane(grid); grid.prefWidthProperty().bind(root.widthProperty()); Rectangle a = new Rectangle(rectaWidth, rectaHeigth); a.setFill(Color.RED); Rectangle b = new Rectangle(rectaWidth, rectaHeigth); b.setFill(Color.BLUE); Rectangle c = new Rectangle(rectaWidth, rectaHeigth); c.setFill(Color.YELLOW); Rectangle[] recta = { a, b, c }; grid.prefWidthProperty().addListener((e) -> { int colums = (int) (((DoubleProperty) e).getValue() / rectaWidth); colums = colums < 1 ? 1 : colums > recta.length ? recta.length % colums : colums; for (int row = 0; row < Math.ceil(((double) recta.length / (double) colums)); row++) { for (int colum = 0; colum < colums; ++colum) { if (((row * colums) + colum) < recta.length) grid.setConstraints(recta[(row * colums) + colum], colum, row); } } }); grid.getChildren().addAll(a, b, c); Scene s = new Scene(root, 500, 500); primaryStage.setScene(s); primaryStage.show(); } }
The listener dynamically monitors the width of the panel and accordingly adjusts the number of columns by moving all the rectangles each time ... The solution with storing the living object position indices in the grid in a certain class model is quite right and using this solution to dynamically add an object to the grid ... This is closer to the second part of the question ...
|