添加和选择 TableView 中的 ChoiceBox

Adding and selecting ChoiceBox that's inside a TableView

所以我尝试为插入到 TableView 中的每个变量添加一个 ChoiceBox。

Users.java:

 Users(String userName , Integer userAge , ChoiceBox employed)
{
    this.userName = userName ;
    this.userAge = userAge ;
    this.employed= employed;
}
//getters and setters

Main.java:

ObservableList<Users>userList = FXCollections.observableArrayList();
TableView<Users> userTable = new TableView();
TableColumn<Users, String> nameCol = new TableColumn();
TableColumn<Users, Integer> ageCol = new TableColumn();

TableColumn<Users, ChoiceBox> employCol = new TableColumn();

private ChoiceBox createBox(){ 
ChoiceBox box = new ChoiceBox();
box.getItems.addAll("true" , "false");
box.setValue("true"); 
return box;
}


userList.addAll(new User("James" , 47 , createBox()));

一切编译和运行都很好,但我无法获得对用户单击的 box 的引用。我尝试通过 selection 模型获取它:

userTable.getSelectionModel().getSelectedItem().employed;

但这只是给了我一个 NullPointerException。我需要能够 select 选择框,如果用户 selects "true" ,那么它将显示在文本字段中,但这是一个简单的修复,我遇到了麻烦实际上将实例获取到选择框。我提到了:

Getting selected item from a JavaFX TableView

ChoiceBox with custom Item in a TableView

但无济于事。我也尝试过使用

的多种变体
ChoiceBoxTableCell

ChoiceBoxTableList

但这些甚至无法编译,我不断收到回调错误。

Callback<TableColumn<Users, String>, TableCell<Users, String>> cellFactory
            = //
            new Callback<TableColumn<Users, String>, TableCell<Users, String>>() {
        @Override
        public TableCell call(final TableColumn<Users, String> param) {
            final TableCell<Users, String> cell = new TableCell<Users, String>() {

                final ChoiceBox createBox(){ 
ChoiceBox box = new ChoiceBox();
box.getItems.addAll("true" , "false");
box.setValue("true"); 

                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        setGraphic(null);
                        setText(null);
                    } else {
                        setGraphic(createBox);
                        setText(null);
                    }
                }
            };
            return cell;
        }
    };

    employCol.setCellFactory(cellFactory);