Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit javafx TableView Size to the Size necessary?

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:

  1. Originally I assumed that there must be a way deactivate the scroll bars. But I couldn't find any way to do that.

  2. 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.

  3. 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()); }

  4. 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!

like image 971
thorwinn Avatar asked Dec 15 '14 13:12

thorwinn


1 Answers

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.

like image 88
b3tuning Avatar answered Nov 15 '22 11:11

b3tuning