0

I need a method of determining the height of each row in a TableView. As you can in the snapshot see below the height of each row vary.

enter image description here

So why do I want to achieve this?

I've got to take the items from an existing TableView and have them placed in other temporary tables so that the number of items in those can fit in exactly without the need for the vertical scrollbars. The temporary tables have got a fixed height of 490. So say that the existing TableView has got 50 items, then the maximum number rows a temporary table can have is 24(I tested this before) given that each row in the existing TableView has only a single line of text. But when there multiples lines of text in the rows, then the maximum number of rows a temporary table can have will differ.

So, the solution I came up with was sum the heights of the rows when being placed in the temporary tables. If the total height gets close to the temporary tables' fixed height(i.e. 490), stop adding items to that table. Then create another temporary table and add the remaining items to that one. Hence, the question "how do I get the height of a single row"?

  • What exactly are you trying to create? A function that, given a TableView, will determine the current height of all the visible rows and return an ordered map of table rows to height values? Or something else? – jewelsea Sep 11 '21 at 05:44
  • I've got to take the items from an existing TableView and have them placed in other temporary tables so that the number of items in those can fit in exactly without the need for the vertical scrollbars. The temporary tables have got a fixed height of 490. So say that the original TableView has got 50 items, then the maximum number rows a temporary table can have is 24(I tested this before) given that each row in the existing TableView has only a single line of text. But when there multiples lines of text in the rows, then the maximum number of rows a temporary table can have will differ. – space-ninja-x Sep 11 '21 at 05:57
  • So, the solution I came up with was sum the heights of the rows when being placed in the temporary tables. If the total height gets close to the, temporary tables' fixed height(i.e. 490), stop adding items to that table. Then create another temporary table and add the remaining items to that one. – space-ninja-x Sep 11 '21 at 06:03
  • 1
    Thanks for explaining that, I was wondering why you wanted this. When there is lots of extra explanatory text, it is best to edit the question to enhance it, usually that works better than using the comments. – jewelsea Sep 11 '21 at 06:16
  • _So, the solution I came up with was sum the heights of the rows when being placed in the temporary tables_ how do you do that (there's no public api, except talking to the skin's virtualFlow) . Or the other way round: if you know the height of a row, what's the problem? [mcve] please .. – kleopatra Sep 11 '21 at 12:27
  • @kleopatra Summing up the heights of the rows is what I thought of. But I don't know how to get the height of a row or even if it can be done. That's the point of the question. Like stated in the question "the height of each row vary" and it's shown in the snapshot. So I don't know what the height of each row is - it's not fixed. – space-ninja-x Sep 11 '21 at 13:24
  • ahh .. okay, misunderstood that paragraph, sry. No, there is no api to access the rows from client code - it's the skin which has access to its flow which has api to access the actual rows. So you could try to implement a custom skin supporting a what you need. Beware: that's expert coding - make sure you understand the fine print :) – kleopatra Sep 11 '21 at 14:59
  • 1
    but then: your requirement sounds .. unusual - what's the reason you need those temporary tables? Feels like a xy-problem. – kleopatra Sep 11 '21 at 15:02
  • @kleopatra Apology accepted. Yes the requirement does sound unusual. And thanks for your information. The reason I'm trying to do this is because I'm trying to print out all the items from an existing TableView on to paper. And when printing items from a TableView only the items that are visible will be printed out. So if there are items that in order to be viewed must be scrolled down - those ones won't appear. That's why I decided to divide all the items from the existing TableView to temporary ones. – space-ninja-x Sep 12 '21 at 02:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237019/discussion-between-user1105-and-kleopatra). – space-ninja-x Sep 12 '21 at 02:16
  • Maybe use a GridPane for your data instead of a TableView. The functionality of GridPane might actually be closer to what you want. – jewelsea Sep 12 '21 at 03:28
  • @jewelsea how would a GridPane be helpful in printing a TableView (which is the x in this xy-problem ;) OP: _Let us continue this discussion in chat_ no never, if you need individual support I would suggest hiring a consultant ;) – kleopatra Sep 12 '21 at 08:40
  • I think you misunderstood. StackOverflow put up a warning saying "Avoid long comments and continue in chat" or somewhat, that's why I had it put there. And the hyperlink "Let us continue this discussion in chat" was auto-generated. If you don't feel comfortable about it, I'll ignore the warning and have it attached as a comment. – space-ninja-x Sep 12 '21 at 08:52
  • @kleopatra Here's what I posted in the chat section: I carried out test prints before and found out that a maximum of 24 rows can be printed out on paper. But this works fine when each row has only 1 line of text. And when there are multiple lines of text in a row (check the image attached), the maximum number of rows a temporary table can have will vary. This is the problem I'm trying to solve. – space-ninja-x Sep 12 '21 at 08:55
  • @jewelsea If I do use a GridPane the data held in each row will be placed in individual grids, yes? For example, in the snapshot above there are 11 rows and 7 columns, so I'll need a 11x7 grid. But then I don't see how it would solve the problem. That's because once again the grid's height will be set to a printable height, and clearly each row's height will vary. – space-ninja-x Sep 12 '21 at 09:02
  • TableView is a virtualized control designed for a UI to be a view into in a window on the data rather than a representation of the data itself. It only shows the visible portion of the data and has support for interactive features like scrolling. GridView doesn't virtualize the data to be a window on the data. Anything you put in a Grid is displayed. When you add new rows to a TableView, the tableview itself doesn't grow, but a grid does. You can easily measure the height of a grid or nodes in the grid and, if needed, calculate the height of a grid row. – jewelsea Sep 12 '21 at 10:44
  • One possible solution is to create a grid, then keep adding rows to it. Each time you add a row, [generate a layout pass](https://stackoverflow.com/questions/26152642/get-the-height-of-a-node-in-javafx-generate-a-layout-pass) and measure the height of the entire grid. If the height goes past the pagination limit, then remove the last row from the grid, create a new grid, then repeat the process. To me GridView seems to a better fit for a printed table of data than a TableView, though by default it will have different styling (with work, you can style the grid if you want). – jewelsea Sep 12 '21 at 10:49
  • The above on GridView vs TableView for printing is just a suggestion and may not fit your requirements, but it was just what seemed to make sense to me. You may not even need to manually split the Grid as outlined above, to get all the data to print and do the pagination, likely the JavaFX printing implementation will already do that for you. I haven't done any work printing with JavaFX, so I am just speculating, but it makes sense to me that, if you just put all of the data in one large GridView, that JavaFX printing would auto-paginate for you. – jewelsea Sep 12 '21 at 10:51
  • repeating the part of my earlier comment that seems to have gotten overlooked: _it's the skin which has access to its flow which has api to access the actual rows. So you could try to implement a custom skin supporting what you need_ – kleopatra Sep 12 '21 at 11:51
  • But you also said that it's expert coding and I haven't done anything like it before (though I know that there's a first for everything). I'm giving the suggestion brought by @jewelsea a try - it does make sense, but I will only have to see how it works after implementation. I can't believe that it takes this much effort to get this task done. – space-ninja-x Sep 12 '21 at 12:00
  • You can add a row factory to get access to rows which the factory creates, in case you need that. I don’t think you need your own custom skin for that. Check the TableView api and google searches for row factory. – jewelsea Sep 13 '21 at 02:48

0 Answers0