to populate a table is easy when you populate it with data from one class, but what is when I want to populate it with data from a OneToMany relationship?
for example:
Class Person: Id, firstname, lastname, cars(cars have a onetomany relationship with Class Cars, so one person can have one or more cars)
Now, if a person got two cars, how can I add this to a table?
It should look something like this:
ID | firstname | lastname | vehicleBrand(from cars)
==============================================
1 | jack | jackson | crysler
----------------------------------------------
| | | bmw
----------------------------------------------
2 | sally | jackson | ford
If I have the observable list from Person I can populate the person data like this:
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person,String>("firstName")
);
but something like:
vehicleBrandCol.setCellValueFactory(
new PropertyValueFactory<Cars,String>("vehicleBrand")
);
or
vehicleBrandCol.setCellValueFactory(
new PropertyValueFactory<Person,String>("vehicleBrand")
);
does not work.
Edit: The answer below works for one brand. If I try to add all I tried it by using a loop like this:
for (Car aCars : cars) {
return new SimpleObjectProperty<>(aCars.vehicleBrand());
}
But the loop does not loop?! (I printed the size of cars and it is not 1)
This should be part of what you are looking for, you can do this:
Person
andCar
.Person
has anArrayList<Car>
of cars.Person
has also a methodgetCars()
which returns this person's collection (theArrayList
) of cars.Now to get the brand from your
Car
class displayed in theTableView
of persons you can set theCellValueFactory
as follows:The
param.getValue()
method returns theperson
passed as a parameter.Notice that in this example I'm returning(displaying) only the first car brand from the collection of cars of the current person, you can try to adapt this example to your case.
No you can't do this because you are declaring a column that should contain
String
objects notList
objects.What you can do is create custom cells that would allow you display all of the
Car
brands of a eachPerson
instance, you can take a look at this example to get a grasp of Cell rendering, and here is my example that could help you in your case:First set the type of the content of cells in
vehicleBrandCol
toArrayList<String>
:private TableColumn<Person, ArrayList<Car>> vehicleBrandCol;
Then set the
CellValueFactory
as follows:vehicleBrandCol.setCellValueFactory(new PropertyValueFactory<Person, ArrayList<Car>>("cars"));
and then you'll need custom cells to display data (Cars) contained within this
ArrayList
, so set theCellFactory
as follows:Notice that I'm creating a
VBox
and using Labels, you can use whateverNode
you like, for example you can use aListView
, or aGridPane
and set them as graphic for theCell
.