How to limit the size of javafx table size to the required size?

I am programming a simple Questionare application. I use a TableView inside a custom javaFX component (representing one question) that is used as a ListCell inside a ListView (used to represent the entire questionnaire).

What I want: each ListCell uses as little space as possible (or necessary), while still ignoring its full content.

What happens: TableView has a default size by default. If the table has more content, it displays its contents in a ScrollPane. If the TableView has less content, it is filled with empty space.


I tried to solve my problem in several ways:

  • I initially suggested that there should be a way to deactivate scrollbars. But I could not find a way to do this.

  • Then I tried to resize the table by changing PreferredSizeProperty: table.setPrefHeight() But I can not find any information about how big the table is. table.setPrefHeight(USE_COMPUTED_SIZE); there was no result that I was hoping for.

  • Then I tried to calculate the necessary height myself, but could not find a way to determine the height of individual rows. Calling this.getTableRow().getHeight() in the update method of a custom TableCell will result in NullPointerExceptions. If the checkbox is set to null, it will still return 0! TableRow<T> tr = this.getTableRow(); if (tr != null) { System.out.println(tr.getHeight()); }

  • Pretty upset, I decided to fix the row height by calling table.setFixedCellSize(20); This greatly limits the point of custom TableCells, but I did not do anything else. However, the height of the table is also not equal to the number of rows times the fixed row height, since the table title must be taken into account. And I could not access the table header or set the size / height of the table headers.

Do you have any suggestions in which I did wrong, or what I missed to achieve my original goal? (Having a ListCell that is as large as necessary to display each row of the table, but not one empty row?) Any help / ideas would be greatly appreciated! Thanks!

+6
source share
2 answers

using Bindings (the example has a view of a table with at least two rows or a table with the size of all the elements 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 a subscription in the list that feeds the table, the size of the table will always match the elements

It really depends on how you configure the cells to represent the list and how you set up the factory cell ... as long as fixedCellProperty () is not set in the ListView, I have no problem with the individual cells in the ListView regulating the size of nodes, which they contain.

+1
source

Take a look at:

 tableView.setColumnResizePolicy(..); 

Maybe this will help you

0
source

Source: https://habr.com/ru/post/979626/


All Articles