Search code examples
javajavafxjava-8treeviewtableview

Can I group rows in a Javafx TableView?


I'd like to make a javafx TableView with rows that may (or may not) have "related" rows. Basically, I think it might be a tableview of tableviews, but (a) I'm not sure that would work and (b) my gut tells me there's an easier way.

Imagine a bill or materials like

item    material    quantity    name
1       wood        1           base
2       wood        4           drawer
        wood        4           drawer sides
        wood        1           drawer base
        hardware    1           pull
        hardware    8           nails
3       aluminum    4           leg
        plastic     1           foot
        hardware    1           screws

Right now, it's sorted by name (base, drawer, leg). If I sort it by material, I want the items to be 3, 1, 2 (or 3, 2, 1): aluminum, wood, wood. I need the "subitems" to remain with the numbered items.

Can I make "complex rows"? (And I don't even know what I'd call it!) Or do I need a custom sort that will keep groups together?


Solution

  • You may want to check out TreeTableView. With it, you may be able to sort by whatever criteria you like, while at the same time having the records grouped by any other criteria (for example by name)

    This component gets its data from a tree structure model built on the TreeItem class.

    With it a root TreeItem can be built for the root object of your bill of materials having as children a TreeItem for each group you want (name of the material?). Then, each of these grouping TreeItem objects should contain as children the corresponding TreeItem children for each of the actual material in the bill of materials being displayed.

    There are multiple good tutorials out there that cover in detail the technical aspects of this:

    Hope this helps.