Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX8:在initialize方法中填充TableView_Java_Javafx_Fxml - Fatal编程技术网

JavaFX8:在initialize方法中填充TableView

JavaFX8:在initialize方法中填充TableView,java,javafx,fxml,Java,Javafx,Fxml,我是JavaFX8新手,我正在尝试使用initialize方法向TableView提供控制器中的一些数据。 我看过很多关于它的话题,尝试了很多东西,但对我来说都不管用。 我看到: 还有很多,但没有一个解决方案适合我 这是我的密码: 班级员工 public class Employee extends Person{ private SimpleIntegerProperty salary; private SimpleObjectProperty<Character> d

我是JavaFX8新手,我正在尝试使用initialize方法向TableView提供控制器中的一些数据。 我看过很多关于它的话题,尝试了很多东西,但对我来说都不管用。 我看到:

还有很多,但没有一个解决方案适合我

这是我的密码: 班级员工

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);
}

...

}
以下是定义用户界面的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>

最后是控制器: **类顾问控制器**

    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");   
    }

}
公共类ConsultHRController扩展DefaultController实现可初始化{
@FXML
VBox顾问;
@FXML
公共表视图表;
@FXML
TableColumn firstNameCol;
@FXML
表列lastNameCol;
刀刀;
简单财产雇员;
@凌驾
公共void初始化(URL位置、ResourceBundle资源){
super.initialize();
dao=(dao)dFact.getEmployeeDAO();
试一试{
employees=dao.findAll();
System.out.println(employees.get());
表=新表视图(员工);
firstNameCol.setCellValueFactory(新属性ValueFactory(“firstName”));
lastNameCol.setCellValueFactory(新属性ValueFactory(“lastName”));
table.getColumns().setAll(firstNameCol,lastNameCol);
System.out.println(firstNameCol.getCellData(0));
}捕获(SQLE异常){
//托多·梅特雷是一个多涅斯之家
e、 printStackTrace();
}
}
公共无效goHRAction(ActionEvent e)引发IOException{
goSmwhereAction((Stage)ConsultHR.getScene().getWindow(),“/fr/kaf/app/fxml/hr/humanresources.fxml”);
}
}

正如您所看到的,我在initialize方法中有一个“System.out.println(firstNameCol.getCellData(0));”。结果显示,单元格不是空的,并且填充了良好的数据,但在我的UI中看不到任何内容。

初始化方法中替换
表格视图

table =new TableView<Employee>(employees);

在哪里分配属性字段???我为每个类都有一个构造函数,我没有把它们放在这里,以避免污染代码的重要部分。我会更新它,让它更清晰。非常感谢!它现在可以工作了,我仍然不明白为什么实例化一个新的TableView不起作用,它应该重新实例化FXML加载程序注入的那个,对吗?谢谢你的建议again@Kyle_jumpen:它用新的
表视图
覆盖字段值。这并不意味着它会以任何方式修改场景。旧的
TableView
仍然是
HBox
的子视图。它类似于以下场景:您有一个
列表
<代码>对象v=list.get(0);v=“你好,世界”此代码段中从不修改列表;只有用于存储第一个元素值的变量的值被修改。我明白了!我不知道注射是怎么起作用的。再次感谢。
table =new TableView<Employee>(employees);
@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();
    }

}