在 javafx 的表视图中重新添加复选框属性



我是JavaFx的新手。我设计了一个表格,在第 1 列中我有一个按钮,在第 2 列中我有一个复选框。单击按钮时,我想获得复选框属性的内容。但问题是我总是得到错误的输出。这是我的代码

型号.java

private boolean Active;     
public boolean isActive() {
return Active;
}   
public void setActive(boolean active) {
Active = active;
}

在我的控制器类中,我设计了下面的 initialize(( 方法

控制器.java

@FXML
private TableView<Model> tableBuilding;
@FXML
private TableColumn<Model,Boolean> colActive;
@FXML
private TableColumn<Model,Model> colAction; 
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
//Containing the button         
colAction.setCellFactory(col -> {
Button ShowButton = new Button("Show");
TableCell<Model, Model> cell = new TableCell<Model, Model>() {
@Override
//Updating with the number of row 
public void updateItem(Model building, boolean empty) {
super.updateItem(building, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(ShowButton);
}
}              
};                                          
ShowButton.setOnAction(event -> {
TableRow row = cell.getTableRow();
Model building=(Model) row.getItem();

ObservableList<Model> data = tableBuilding.getItems();
for (Model item : data){
//check the boolean value of each item to determine checkbox state
System.out.println(item.isActive());
}
});
return cell ;
});
//Containing the checkbox
colActive.setCellValueFactory(new PropertyValueFactory<Model,Boolean>("Active"));
colActive.setCellFactory(tc -> new CheckBoxTableCell<>());   
}

在这里System.out.println(item.isActive());总是将 false 重新调谐为输出。如何获取该复选框的实际属性?谁能帮我?

我相信你可以稍微简化一下你的控制器。尝试将控制器代码更改为:

public class Controller {
@FXML
private TableView<Model> tableBuilding;
@FXML
private TableColumn<Model, Boolean> colActive;
@FXML
private TableColumn<Model, Model> colAction;
@FXML
public void initialize() {
// Sample List
ObservableList<Model> models = FXCollections.observableArrayList();
models.addAll(
new Model(),
new Model(),
new Model()
);
tableBuilding.setItems(models);
//Containing the button
colAction.setCellFactory(col -> {
Button ShowButton = new Button("Show");
TableCell<Model, Model> cell = new TableCell<>() {
@Override
//Updating with the number of row
public void updateItem(Model building, boolean empty) {
super.updateItem(building, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(ShowButton);
}
}
};
ShowButton.setOnAction(event -> {
TableRow row = cell.getTableRow();
Model building = (Model) row.getItem();
System.out.println(building.getActive());
});
return cell;
});
//Containing the checkbox
colActive.setCellValueFactory(new PropertyValueFactory<>("active"));
colActive.setCellFactory(col -> new CheckBoxTableCell<>());
}
}

具体来说,最后两行代码;您不需要指定自己的TableCell因为CheckBoxTableCell自行处理Model的更新。

但是,需要更改Model类以将Active字段更新为 true 属性;否则,TableView无法正确更新它。 控制器中的行colActive.setCellValueFactory(new PropertyValueFactory<Model,Boolean>("Active"));告诉单元的值绑定到Model类中名为Active的 JavaFX 属性,但没有这样的属性。

以下是定义属性的正确方法:

public class Model {
private SimpleBooleanProperty active = new SimpleBooleanProperty(true);
public boolean getActive() {
return active.get();
}
public SimpleBooleanProperty activeProperty() {
return active;
}
public void setActive(boolean active) {
this.active.set(active);
}
}

有趣的是,这似乎是遵循正确的 Java 编码实践很重要的情况之一。Active属性在这里也应该是小写的,而不是大写的。

最新更新