我创建了一个用TableView
填充的ObservableList
,
我的表的某些列是CheckBox
,我想要的是CheckBox
与我创建的模型对象绑定。
到目前为止,我得到的结果是:
如果是文字,我可以看到结果,但如果CheckBox
始终未选中
答案 0 :(得分:0)
tableview中复选框的示例:
表格中显示的项目:会员
public class Member {
private StringProperty myName;
private BooleanProperty myCheck;
public Member(String name, boolean checked) {
myName = new SimpleStringProperty(name);
myCheck = new SimpleBooleanProperty(checked);
}
public StringProperty nameProperty() { return myName; }
public BooleanProperty checkProperty() { return myCheck; }
}
表:(现在使用内置类CheckBoxTableCell,感谢James.D)
ObservableList<Member> members = FXCollections.observableArrayList();
members.add(new Member("peter", true));
members.add(new Member("gernot", true));
members.add(new Member("fritz", false));
for (int i = 1000; i < 2000; i++) members.add(new Member("N"+i, (i%5==0)));
TableView<Member> table = new TableView<Member>();
table.prefHeightProperty().bind(box.heightProperty());
table.setItems(members);
TableColumn<Member,String> c1 = new TableColumn<Member,String>("Name");
c1.setCellValueFactory(new PropertyValueFactory<Member,String>("name"));
table.getColumns().add(c1);
TableColumn<Member,Boolean> c2 = new TableColumn<Member,Boolean>("Membership");
c2.setCellValueFactory(new PropertyValueFactory<Member,Boolean>("check"));
c2.setCellFactory(column -> new CheckBoxTableCell());
table.getColumns().add(c2);
pane.getChildren().addAll(table);
结果: