You can't create your columns this way. TableView
constructor takes an ObservableList
as its parameter, but it expects to find there table values, in other words, your rows.
I'm afraid that there isn't any generic way to add items to your table, because each table is more or less coupled to its data model. Let's say that you have a Person
class which you want to display.
public class Person {
private String name;
private String surname;
public Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
}
In order to do that you'll have to create the table and its two columns. PropertyValueFactory
will fetch the necessary data from your object, but you have to make sure that your fields have an accessor method that follows the standard naming convention (getName()
, getSurname()
, etc). Otherwise it will not work.
TableView tab = new TableView();
TableColumn nameColumn = new TableColumn("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn surnameColumn = new TableColumn("Surname");
surnameColumn.setCellValueFactory(new PropertyValueFactory<>("surname"));
tab.getColumns().addAll(nameColumn, surnameColumn);
Now all you have to do is to create your Person
object and add it to the table items.
Person person = new Person("John", "Doe");
tab.getItems().add(person);
Table1.getItems()
is where you can add rows. – KerrisonTableView
as aTableView
column. – Missy