属性 JavaFX TableView 中的绑定不起作用
Property bindings in JavaFX TableView are not working
我点击了很多链接,找到了在 table 视图中显示复选框的解决方案。但是我无法在 table 视图中更改复选框的值。
Link 我关注了:
How to add CheckBox's to a TableView in JavaFX
型号Class:
public class TUser {
private SimpleStringProperty name;
private SimpleStringProperty address;
private SimpleBooleanProperty active;
public TUser(String name, String address, boolean active) {
this.name = new SimpleStringProperty(name);
this.address = new SimpleStringProperty(address);
this.active = new SimpleBooleanProperty(active);
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name = new SimpleStringProperty(name);
}
public String getAddress() {
return address.get();
}
public void setAddress(String address) {
this.address = new SimpleStringProperty(address);
}
public boolean getActive() {
return active.get();
}
public void setActive(boolean active) {
this.active = new SimpleBooleanProperty(active);
}
}
FXML 文件:
<TableView fx:id="usertable" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<columns>
<TableColumn fx:id="name" prefWidth="142.0" text="Name" />
<TableColumn fx:id="address" prefWidth="147.0" text="Address" />
<TableColumn fx:id="active" prefWidth="153.0" text="Active" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
控制器Class:
public class UserManagement extends AnchorPane {
@FXML
private TableView<TUser> usertable;
@FXML
private TableColumn<TUser, String> address;
@FXML
private TableColumn<TUser, String> name;
@FXML
private TableColumn<TUser, Boolean> active;
public UserManagement() throws IOException {
initGraphics();
initTable();
}
private class CheckBoxCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
@Override public TableCell<S, T> call(TableColumn<S, T> p) {
return new CheckBoxTableCell<>();
}
}
private void initGraphics() throws IOException {
FXMLLoader content = new FXMLLoader(getClass().getResource("/mypack/fxmls/UserManagement.fxml"));
content.setController(this);
Node contentnode = (Node) content.load();
AnchorPane.setBottomAnchor(contentnode, 0.0);
AnchorPane.setLeftAnchor(contentnode, 0.0);
AnchorPane.setRightAnchor(contentnode, 0.0);
AnchorPane.setTopAnchor(contentnode, 0.0);
getChildren().add(contentnode);
active.setCellFactory(new CheckBoxCellFactory<TUser, Boolean>());
address.setCellValueFactory(new PropertyValueFactory<TUser, String>("address"));
name.setCellValueFactory(new PropertyValueFactory<TUser, String>("name"));
active.setCellValueFactory(new PropertyValueFactory<TUser, Boolean>("active"));
}
private void initTable(){
ObservableList<TUser> data = FXCollections.observableArrayList(new TUser("ABC", "ABC Road", true));
usertable.setItems(data);
}
}
输出:
要使复选框正确绑定到模型 class 中的 属性,您需要定义一个 属性 访问器方法:
public class TUser {
// other methods and fields as before....
public BooleanProperty activeProperty() {
return active ;
}
}
为您的其他属性也定义类似的方法可能是个好主意。
请注意,您的 table 不是 editable,因此用户无法操作复选框。如果你想让它们成为 editable,那么让 TableView
editable:
<TableView fx:id="usertable" editable="true" ...>
TableColumn
默认为 editable。
我点击了很多链接,找到了在 table 视图中显示复选框的解决方案。但是我无法在 table 视图中更改复选框的值。
Link 我关注了: How to add CheckBox's to a TableView in JavaFX
型号Class:
public class TUser {
private SimpleStringProperty name;
private SimpleStringProperty address;
private SimpleBooleanProperty active;
public TUser(String name, String address, boolean active) {
this.name = new SimpleStringProperty(name);
this.address = new SimpleStringProperty(address);
this.active = new SimpleBooleanProperty(active);
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name = new SimpleStringProperty(name);
}
public String getAddress() {
return address.get();
}
public void setAddress(String address) {
this.address = new SimpleStringProperty(address);
}
public boolean getActive() {
return active.get();
}
public void setActive(boolean active) {
this.active = new SimpleBooleanProperty(active);
}
}
FXML 文件:
<TableView fx:id="usertable" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<columns>
<TableColumn fx:id="name" prefWidth="142.0" text="Name" />
<TableColumn fx:id="address" prefWidth="147.0" text="Address" />
<TableColumn fx:id="active" prefWidth="153.0" text="Active" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
控制器Class:
public class UserManagement extends AnchorPane {
@FXML
private TableView<TUser> usertable;
@FXML
private TableColumn<TUser, String> address;
@FXML
private TableColumn<TUser, String> name;
@FXML
private TableColumn<TUser, Boolean> active;
public UserManagement() throws IOException {
initGraphics();
initTable();
}
private class CheckBoxCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
@Override public TableCell<S, T> call(TableColumn<S, T> p) {
return new CheckBoxTableCell<>();
}
}
private void initGraphics() throws IOException {
FXMLLoader content = new FXMLLoader(getClass().getResource("/mypack/fxmls/UserManagement.fxml"));
content.setController(this);
Node contentnode = (Node) content.load();
AnchorPane.setBottomAnchor(contentnode, 0.0);
AnchorPane.setLeftAnchor(contentnode, 0.0);
AnchorPane.setRightAnchor(contentnode, 0.0);
AnchorPane.setTopAnchor(contentnode, 0.0);
getChildren().add(contentnode);
active.setCellFactory(new CheckBoxCellFactory<TUser, Boolean>());
address.setCellValueFactory(new PropertyValueFactory<TUser, String>("address"));
name.setCellValueFactory(new PropertyValueFactory<TUser, String>("name"));
active.setCellValueFactory(new PropertyValueFactory<TUser, Boolean>("active"));
}
private void initTable(){
ObservableList<TUser> data = FXCollections.observableArrayList(new TUser("ABC", "ABC Road", true));
usertable.setItems(data);
}
}
输出:
要使复选框正确绑定到模型 class 中的 属性,您需要定义一个 属性 访问器方法:
public class TUser {
// other methods and fields as before....
public BooleanProperty activeProperty() {
return active ;
}
}
为您的其他属性也定义类似的方法可能是个好主意。
请注意,您的 table 不是 editable,因此用户无法操作复选框。如果你想让它们成为 editable,那么让 TableView
editable:
<TableView fx:id="usertable" editable="true" ...>
TableColumn
默认为 editable。