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!
TableView
is not supposed to be used for layout, which is effectively what you're trying to do here. Do you actually need any of the functionality of a table view (resizable columns, draggable columns, sorting on header click, etc)? If not it might be better to use a layout pane (e.g. aGridPane
) to display the cell instead of aTableView
. – Calcine