I am programming a simple Questionare Application. I am using a TableView inside a custom javaFX component (representing one question), that is used as a ListCell inside a ListView (used to represent the entire questionaire).
What I want: Each ListCell uses as little space as possible (or necessary) while still desplaying its full content.
What happens: The TableView seems to default to a standard size. If the Table has more content, it displays its content within a ScrollPane. If the TableView has less content, it fills itself with empty space.
I tried to solve my problem in a number of ways:
Originally I assumed that there must be a way deactivate the scroll bars. But I couldn't find any way to do that.
Then I tried to change the size of the table by changing the PreferredSizeProperty:
table.setPrefHeight()
But I can't find any information on how big the table is.
table.setPrefHeight(USE_COMPUTED_SIZE);
did not have the result I was hoping for.
Then I tried to compute the necessary height myself, but I couldn't find any way to determine the height of the individual rows. Calling this.getTableRow().getHeight()
within the update method of the custom TableCell would result in NullPointerExceptions. When checked against null, it would still return 0!
TableRow<T> tr = this.getTableRow();
if (tr != null) {
System.out.println(tr.getHeight());
}
Quite frustrated, I decided to fix the row height, by calling
table.setFixedCellSize(20);
This severly limits the point of custom TableCells, but I wasn't making any progress otherwise. However, the table height is also not equal to the number of lines multiplied by a fixed row height, because the table header needs to be taken into account. And I couldn't access the table header or ask for the table headers size / height.
Do you have any suggestions where I went wrong or what I have overlooked to achieve my original goal (Having a ListCell that is preciesly as big a as necessary to display every line of the table, but not a single empty line?) Any help / ideas would be most welcome! Thank you!
with the use of Bindings (example has a table view with a minimum of 2 rows, or a table the size of all items in the list...
/**
* Helper to set table height based on content
*
* @param table - the table in context
* @param rowHeight - the height of a single row, alternatively could use table.fixedCellSizeProperty()
* @param headerHeight - the height of the table header
* @param margin - a value for the margins
*/
public void tableHeightHelper(TableView<?> table, int rowHeight, int headerHeight, int margin) {
table.prefHeightProperty().bind(Bindings.max(2, Bindings.size(table.getItems()))
.multiply(rowHeight)
.add(headerHeight)
.add(margin));
table.minHeightProperty().bind(table.prefHeightProperty());
table.maxHeightProperty().bind(table.prefHeightProperty());
}
with a listener or subscription on the list feeding the table the table size will always adjust to the items
It really depends on how you are customizing the cells for the list view, and how you've configured the cell factory... as long as fixedCellProperty() in the ListView isn't set, I haven't had an issue with individual cell's in a ListView adjusting to the size of the nodes they contain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With