I'm new to JavaFX 8 and I am trying to feed a TableView with some data in the controller using the initialize method. I have seen a lot of topic on it, try a lot of stuff but it didn't work for me. I've seen :
我是JavaFX 8的新手,我正在尝试使用initialize方法在控制器中为TableView提供一些数据。我已经看过很多关于它的主题,尝试了很多东西,但它对我不起作用。我见过 :
- How to populate TableView dynamically with FXML and JavaFX
- How to populate a TableView that is defined in an fxml file that is designed in JavaFx Scene Builder
- Javafx PropertyValueFactory not populating Tableview
如何使用FXML和JavaFX动态填充TableView
如何填充在JavaFx Scene Builder中设计的fxml文件中定义的TableView
Javafx PropertyValueFactory没有填充Tableview
and a lot more but none of the solutions happend to work for me.
还有更多,但没有一个解决方案适合我。
Here is my code : class Employee
这是我的代码:类Employee
public class Employee extends Person{
private SimpleIntegerProperty salary;
private SimpleObjectProperty<Character> droit;
public Employee(){
super();
this.salary = new SimpleIntegerProperty();
this.droit = new SimpleObjectProperty<Character>();
}
public Employee(int id, String firstName, String lastName, String password, char droits, int salary) {
super(id,firstName,lastName,password);
this.salary = new SimpleIntegerProperty(salary);
this.droit = new SimpleObjectProperty<Character>(droits);
}
public Employee(String firstName, String lastName, String password, char droits, int salary) {
super(firstName,lastName,password);
this.salary = new SimpleIntegerProperty(salary);
this.droit = new SimpleObjectProperty<Character>(droits);
}
...
}
class Person
public class Person {
protected SimpleStringProperty firstName;
protected SimpleStringProperty lastName;
public Person(){
this.firstName = new SimpleStringProperty();
this.lastName = new SimpleStringProperty();
}
public Person(String firstName, String lastName){
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty(){
return this.firstName;
}
public String getLastName() {
return this.lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty(){
return this.lastName;
}
Here is the FXML that defines the User Interface :
以下是定义用户界面的FXML:
ConsultHR.fxml
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox fx:id="ConsultHR" maxHeight="600.0" maxWidth="600.0" minHeight="500.0" minWidth="500.0" prefHeight="550.0" prefWidth="550.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fr.kaf.app.controller.hr.ConsultHRController">
<children>
<HBox maxHeight="450.0" minHeight="450.0" prefHeight="450.0">
<children>
<TableView fx:id="table" prefHeight="426.0" prefWidth="288.0">
<columns>
<TableColumn fx:id="lastNameCol" prefWidth="131.0" text="Nom" />
<TableColumn fx:id="firstNameCol" prefWidth="154.0" text="Prénom" />
</columns>
</TableView>
</HBox>
<ButtonBar prefHeight="40.0" prefWidth="400.0">
<buttons>
<Button mnemonicParsing="false" onAction="#goHRAction" text="Rssources Humaines" />
</buttons>
<VBox.margin>
<Insets right="15.0" />
</VBox.margin>
</ButtonBar>
</children>
</VBox>
And finally Here is the Controller : ** class ConsultHRController **
最后这里是Controller:**类ConsultHRController **
public class ConsultHRController extends DefaultController implements Initializable{
@FXML
VBox ConsultHR;
@FXML
public TableView<Employee> table;
@FXML
TableColumn<Employee,String> firstNameCol;
@FXML
TableColumn<Employee,String> lastNameCol;
DAO<Employee> dao;
SimpleListProperty<Employee> employees;
@Override
public void initialize(URL location, ResourceBundle resources){
super.initialize();
dao = (DAO<Employee>) dFact.getEmployeeDAO();
try {
employees = dao.findAll();
System.out.println(employees.get());
table =new TableView<Employee>(employees);
firstNameCol.setCellValueFactory(new PropertyValueFactory<Employee, String>("firstName"));
lastNameCol.setCellValueFactory(new PropertyValueFactory<Employee, String>("lastName"));
table.getColumns().setAll(firstNameCol, lastNameCol);
System.out.println(firstNameCol.getCellData(0));
} catch (SQLException e) {
// TODO Mettre une popup erreur base de données
e.printStackTrace();
}
}
public void goHRAction(ActionEvent e) throws IOException{
goSmwhereAction((Stage) ConsultHR.getScene().getWindow(),"/fr/kaf/app/fxml/hr/HumanRessources.fxml");
}
}
As you can see I have a "System.out.println(firstNameCol.getCellData(0));" in the initialize method. It results in giving me that the cell is not empty and it's filled in with the good data but I don't see anything in my UI.
如您所见,我有一个“System.out.println(firstNameCol.getCellData(0));”在initialize方法中。它导致我发现单元格不是空的,并且填充了良好的数据,但我的UI中没有看到任何内容。
1 个解决方案
#1
2
You replace your TableView
in your initialize
method.
您可以在initialize方法中替换TableView。
table =new TableView<Employee>(employees);
You assign the data to the new TableView
and leave the one created from the fxml empty.
您将数据分配给新的TableView,并将从fxml创建的数据保留为空。
Instead use the one injected by the FXMLLoader
:
而是使用FXMLLoader注入的那个:
@Override
public void initialize(URL location, ResourceBundle resources){
super.initialize();
dao = (DAO<Employee>) dFact.getEmployeeDAO();
try {
employees = dao.findAll();
// set data for the table created by the FXMLLoader
table.setItems(employees);
// no need to add them to the table since the FXMLLoader is ready doing that
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
} catch (SQLException e) {
// TODO Mettre une popup erreur base de données
e.printStackTrace();
}
}
#1
2
You replace your TableView
in your initialize
method.
您可以在initialize方法中替换TableView。
table =new TableView<Employee>(employees);
You assign the data to the new TableView
and leave the one created from the fxml empty.
您将数据分配给新的TableView,并将从fxml创建的数据保留为空。
Instead use the one injected by the FXMLLoader
:
而是使用FXMLLoader注入的那个:
@Override
public void initialize(URL location, ResourceBundle resources){
super.initialize();
dao = (DAO<Employee>) dFact.getEmployeeDAO();
try {
employees = dao.findAll();
// set data for the table created by the FXMLLoader
table.setItems(employees);
// no need to add them to the table since the FXMLLoader is ready doing that
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
} catch (SQLException e) {
// TODO Mettre une popup erreur base de données
e.printStackTrace();
}
}