您的位置:首页 > 编程语言 > Java开发

javaFx中tableview使用cellfactory进行赋值的问题

2016-05-24 11:31 537 查看
在javafx中,可以使用cellfactory来进行赋初值

下面我将举例进行说明

首先当对应的fxml文件被调用时,他里面的相对应的controller中的intiablize方法将被自动调用,

    @FXML

    private void initialize() {

         // Initialize the person table with the two columns.

        firstNameColumn.setCellValueFactory(

                cellData -> cellData.getValue().firstNameProperty());

        lastNameColumn.setCellValueFactory(

                cellData -> cellData.getValue().lastNameProperty());

        // Clear person details.

        showPersonDetails(null);

        // Listen for selection changes and show the person details when changed.

        personTable.getSelectionModel().selectedItemProperty().addListener(

                (observable, oldValue, newValue) -> showPersonDetails(newValue));

    }

需要添加@Fxml以让程序能识别。

其中的cellData是我们同过mainAPP把值传给了tableBiew,cellFactory将tableView中的每一行都填充数据。

mainAPP中将值传给tableVIew

    controller.setMainApp(this);

    public void setMainApp(MainApp mainApp) {

        this.mainApp = mainApp;

        // Add observable list data to the table

        personTable.setItems(mainApp.getPersonData());

    }

  public ObservableList<Person> getPersonData() {

        return personData;

    }

    public MainApp() {

        // Add some sample data

        personData.add(new Person("Hans", "Muster"));

        personData.add(new Person("Ruth", "Mueller"));

        personData.add(new Person("Heinz", "Kurz"));

        personData.add(new Person("Cornelia", "Meier"));

        personData.add(new Person("Werner", "Meyer"));

        personData.add(new Person("Lydia", "Kunz"));

        personData.add(new Person("Anna", "Best"));

        personData.add(new Person("Stefan", "Meier"));

        personData.add(new Person("Martin", "Mueller"));

    }

就是这么个流程,欢迎大家批评指正,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: