How to limit javafx TableView Size to the Size necessary?
Asked Answered
R

1

6

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!

Richelieu answered 15/12, 2014 at 13:50 Comment(5)
So you have crammed this together: ListView -> ListCell -> TableView? This is wrong on so many levels.. I do not know where to start. I strongly recommend you to rewrite the UI with different components. ListView and TableView are both meant to display huge amounts of data and thus layout its children (cells) in a virtualized way. It is not meant to display a single question containing a TableView as a ListCell graphic.Tillett
A Question is repressented by a custom component that uses a TableView as subcomponent. Each question is in itself a ListCell, to be displayed in a ListView. The TableView is not big in my case: it may contain anything from 1 to 10 lines. I agree: For one line it seems somewhat pointless, but if several lines are needed, I don't yet see what is wrong with it.Richelieu
What you're looking to do is going to be pretty difficult (if it's even possible). A 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. a GridPane) to display the cell instead of a TableView.Calcine
If it is not possible, i have to use another solution, obviously. However, I ask users for lists, e.g. for a number of Dates: "List the Dates of your previous 5 appointments" or "List all your previous adresses where you lived longer than one month and the period of time when you lived there". And I thought a table to enter such data would be a pretty obvious choice.Richelieu
Possible duplicate of JavaFX - Adapt TableView height to number of rowsBoomerang
G
1

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.

Garbanzo answered 23/10, 2016 at 10:5 Comment(1)
When would the call to this function happen and why?Freak

© 2022 - 2024 — McMap. All rights reserved.