JavaFX layout with parent extensions
I use JavaFX instead of swing in my project because of enhanced multimedia, webviewer and the possibility of using visual effects However, I learned from the Internet( http://docs.oracle.com/javafx/2/layout/jfxpub-layout.htm And others) found that the JavaFX layout manager focuses on scaling the size of the parent according to the size of the content, while swing focuses on scaling the content according to the size of the content Parent, at least based on the layout being used
Now I'm using the accordion and some titledpane children One of them contains gridpane for a simple reason. This is the best way I know to simulate GridLayout (as I learned from the previous question: JavaFX layout equivalent to GridLayout) I want to divide the contents of titledpane into two rows and one column. Each row uses 50% of the available space height in titledpane (using stage or scene scaling), which is equivalent to a GridLayout (2,1) borderlayout Center will complete To do this, I added a gridpane with two rowconstraints using setpercentheight (50) and a columnconstraint using setpercentwidth (100) However, I notice that the content is scaling the grid appropriately, but the grid does not take up all the available space in titledpane (because it is obviously not like the center of borderpane) I also try to use setmaxwidth to extend the content to the parent, but it doesn't seem to work (JavaFX: how to make my custom component use all available space from parent layout?) Even if it does, do I need to set the maximum width to each descendant in my UI element tree to scale them all?
Either way, does anyone know how to make a titledpane with two equal spaces under the other, proportional to the size of the title pane?
Solution
In fact, your gridpane is growing to fill all its parents
Accordion accordion = new Accordion(); TitledPane titledPane = new TitledPane(); titledPane.setText("Title"); GridPane gridPane = new GridPane(); gridPane.setStyle("-fx-background-color:red"); gridPane.add(new TextArea("Hello"),0); gridPane.add(new TextArea("World"),1); titledPane.setContent(gridPane); accordion.getPanes().add(titledPane);
If this code is executed, gridpane will populate all its parents (check the red across all header pane contents)
However, the contents of gridpane will not fill all columns If you try to resize the window, you will see that the width of textares does not change with gridpane To solve this problem, you need to tell the gridpane that the first column grows with the gridpane itself This is done by adding the following constraints:
ColumnConstraints columnConstraints = new ColumnConstraints(); columnConstraints.setFillWidth(true); columnConstraints.setHgrow(Priority.ALWAYS); gridPane.getColumnConstraints().add(columnConstraints);