I'm pretty new at JavaFX and i'm trying basic functionalities. I'm also using SceneBuilder (FXML) to create my views.
I created a TableView
with SceneBuilder, with no columns (will be added in code) with id tablePeople
. The goal is to store people into this table, like many examples of basic use of TableView
.
Here is my Person
class (I want to store every properties of this class in the table) :
public class Person {
private SimpleStringProperty name;
private SimpleStringProperty forename;
private ObjectProperty<LocalDate> date;
private SimpleStringProperty message;
public Person(String name, String forename, LocalDate date, String message) {
this.name = new SimpleStringProperty(name);
this.forename = new SimpleStringProperty(forename);
this.date = new SimpleObjectProperty<LocalDate>(date);
this.message = new SimpleStringProperty(message);
}
// all getters/setters here
}
I correctly get my TableView from FXML:
@FXML
private TableView<Person> tablePeople;
At the start of the application (after loading FXML and before showing the stage), I fill my TableView
with random datas by using this method I created in FXML controller:
public void initParametersView() {
tablePeople.setEditable(true);
tablePeople.setDisable(false);
String dateStr = "01.01.2001";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate date = null;
date = LocalDate.parse(dateStr,formatter);
ObservableList<Person> people = FXCollections.observableArrayList(
new Person("Name1", "Forename1", date, "a"),
new Person("Name2", "Forename2", date, "b"),
new Person("Name3", "Forename3", date, "c"),
new Person("Name4", "Forename4", date, "d")
);
tablePeople.setItems(people);
TableColumn<Person, String> tcName = new TableColumn<>("Name");
tcName.setCellValueFactory(new PropertyValueFactory<Person,String>("name"));
tablePeople.getColumns().add(tcName);
TableColumn<Person, String> tcForename = new TableColumn<>("Forename");
tcForename.setCellValueFactory(new PropertyValueFactory<Person,String>("forename"));
tablePeople.getColumns().add(tcForename);
TableColumn<Person, LocalDate> tcDate = new TableColumn<>("Date");
tcDate.setCellValueFactory(new PropertyValueFactory<Person,LocalDate>("date"));
tablePeople.getColumns().add(tcDate);
TableColumn<Person, String> tcMessage = new TableColumn<>("Message");
tcMessage.setCellValueFactory(new PropertyValueFactory<Person,String>("message"));
tablePeople.getColumns().add(tcMessage);
}
As you can see, through this:
- I create an ObservableList of Person which is given to the table.
- I create each column which are (should...) using Person's properties values and add it to the
TableView
RESULT :
I get the TableView with the columns I created but datas are not displayed. However, datas seems to have been put into the table, because I can select the first 4 lines of the table (4 instances of Person were created). You can see it here:
What I tried
- I first tried to create my columns with SceneBuilder, get them in my controller and set cell values directly, but same result.
- I tried functions on table/columns like
setEditable
,refresh
, but still no data displayed :-(
Where am I wrong?
Thank you very much for the help.