How to make nested JavaFX GridPanes layout correctly?


Question

Context: I am building a JavaFX application with java1.7.0_07 and Jython. My application has a ScrollPane that contains a GridPane that contains a variable number of GridPanes. My application discards and rebuilds the innermost GridPanes each time a TreeView selection changes.

Problem: When I select something in the TreeView, the innermost GridPanes are huge, pushing all but the first innermost GridPane outside of the viewable area of the the ScrollPane. Regardless, the ScrollPane does not display scroll bars, giving the impression that there is only one innermost GridPane object. Manually resizing the window that contains the ScrollPane 'fixes' things! How can I get the innermost GridPanes to be that size from the moment that I add them?

Of course, the first thing I tried was to call requestLayout() on the outer GridPane from the Jython interpreter, and this works just as well as resizing the containing window. This is also how I discovered that the innermost GridPanes were huge---I called getBoundsInParent() on each of the innermost GridPanes.

However, inside of my ChangeListener.changed() implementation, everything is different. Calling requestLayout() does not seem to do anything. Moreover, the bounds of each of my innermost GridPanes is [0,0,0,0]! It looks like there is something asynchronous going on here, but I am new to JavaFX and I do not know what.

Update: Getting closer! Each of the innermost GridPanes contains a Label, and I am calling setWrapText(true) on each of the Labels. If I do not enable wrapping on the Labels, the layout behaves correctly! If I call setPrefHeight(40) on each of the Labels, the layout behaves correctly even with text wrapping enabled, but I really dislike the idea of trying to guess an appropriate preferred height to assign to each of my Labels. I just want the Labels to start with the bounds that they assume when I 'wiggle' the containing window. But how?

1
2
11/8/2012 9:19:04 PM

Accepted Answer

Each of the innermost GridPane objects had a ColumnConstraints object with a percentWidth of 100. Removing the code that added this (unnecessary) ColumnConstraints object to the innermost GridPane objects fixed the layout misbehavior that appeared only when the contained Label objects had wrapText set to true.

2
11/9/2012 6:46:10 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon