0

Not sure if variables is the right word here...

I have a tableview that is to be populated with Client objects. Each Client object has an int id, a companyName, a firstName and a lastName The tableview only has 2 columns (one for the clientId and the other for the name of the client).

Now I was able to construct the table and have it auto populate with the Clients' id's and companyNames but I end up in a situation where if a client doesn't have a companyName, then that particular cell will not show any text. I was wondering, is there a way to have the tableview use the Client's firstName or lastName given that the Client doesn't have a companyName?

Any advice will be greatly appreciated and I appologize if I wasn't able to ask a proper question.

I can try removing the Client's companyName, firstName and lastName sections and replace them with just a single name category but that's a rather uninspired solution to the problem.

  • 2
    One option: Use a `TableColumn` and then set a custom `cellFactory` where your `TableCell` implementation does the desired logic in the overridden `updateItem(T,boolean)` method (by setting the `text` and/or `graphic` property of the cell appropriately). – Slaw Nov 18 '22 at 02:43
  • 1
    This question is a variant of [How can I Populate a ListView in JavaFX using Custom Objects?](https://stackoverflow.com/questions/36657299/how-can-i-populate-a-listview-in-javafx-using-custom-objects/36657553#36657553). Even though the linked variant uses a ListView the principle of using a cell factory as outlined by Slaw’s comment is the same for a TableView. – jewelsea Nov 18 '22 at 06:15
  • 2
    alternatively to the approach suggested by @Slaw you can use a custom cellValueFactory, see f.i. https://stackoverflow.com/questions/74255691/javafx-how-do-i-fill-a-table-view-with-2-different-classes-with-the-same-super/74256594#74256594 – kleopatra Nov 18 '22 at 12:44

0 Answers0