If I create a ListView:
new ListView<>(FXCollections.observableArrayList("1", "2", "3"))
I would expect it to create a ListView with 3 rows. But it doesn't. It creates a ListView of 17 or so rows. Is there a way to tell ListView to always be the height such that whatever items are in it are always shown but no blank rows?
Having it auto width would also be useful, so it's always as wide as the widest row.
One purpose of this is that then it could be used in a ScrollPane. I know it has its own scrollbars, but they don't offer sufficient control.
StackOverflow's reputation system is preventing me from commenting on Paul Marshall's answer, but I wanted to add for anyone else looking at this that his 24px estimate for rows is "generally" confirmed by the official JavaFX documentation - see "fixedCellSize" at http://download.java.net/jdk8/jfxdocs/javafx/scene/control/ListView.html:
So while I agree that "[f]iguring out row height, border width, etc., somehow from styling or tricky stuff is messy, but probably the only way to do it" may be true, starting with an assumption that is backed up by official documentation is a good place to start, and seems to result in decent-looking lists in my testing on a ListView (using Java 7).
I just found out that Paul Marshall's answer can be reducted to a one-liner using Bindings.size that creates a numeric jfx property representing the size of an ObservableList :
The list cell height must sadly still be hardcoded AFAIK.
Try to setPrefHeight(double) in subclass of ListCell. For example in my code
Are you searching for that:
This will hide empty cell's.
Example Image to set ListView Height
If you have a static list , You can set the list view height with the setter method that comes with the ListView class setPrefHeight
After working hard and based in the @warakawa answer, I found the solution. As we all know, the implementation of the Listview is causing many headaches, because of this I wrote two classes that fixes the problem of "PREF_SIZE" (which has a constant of 400 for the height and width is calculated according to height). The skin class that I wrote calculates the size as we expect and also prevents ugly horizontal bar when the new property "fillWidth" is changed to "true", this property causes the cells to grow horizontally as much as possible. Regards.
ListViewFixed.java
ListViewFixedSkin.java