您还没有真正描述数据的结构,但看起来有某种Strings (companyGroups) 的集合,并且每一行都是表格由String (@ 987654324@) 和 companyGroups 的每个元素一个布尔值。因此,一种方法就是在模型类AttributeRow 中定义一个Map<String, BooleanProperty>,其中映射中的键是companyGroups 的一个元素:
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class AttributeRow {
private final StringProperty name = new SimpleStringProperty();
private final Map<String, BooleanProperty> activeByGroup = new HashMap<>();
public AttributeRow(List<String> companyGroups) {
for (String group : companyGroups) {
activeByGroup.put(group, new SimpleBooleanProperty()) ;
}
}
public final BooleanProperty activeProperty(String group) {
// might need to deal with the case where
// there is no entry in the map for group
// (else calls to isActive(...) and setActive(...) with
// a non-existent group will give a null pointer exception):
return activeByGroup.get(group) ;
}
public final boolean isActive(String group) {
return activeProperty(group).get();
}
public final void setActive(String group, boolean active) {
activeProperty(group).set(active);
}
public final StringProperty nameProperty() {
return this.name;
}
public final String getName() {
return this.nameProperty().get();
}
public final void setName(final String name) {
this.nameProperty().set(name);
}
}
列的单元格值工厂没有什么特别之处 - 它仍然只需将每一行映射到列的适当可观察属性:
for (String group : groups) {
TableColumn<AttributeRow, Boolean> groupColumn = new TableColumn<>(group);
groupColumn.setCellFactory(CheckBoxTableCell.forTableColumn(groupColumn));
groupColumn.setCellValueFactory(cellData -> cellData.getValue().activeProperty(group));
attributeTable.getColumns().add(groupColumn);
}
当然要更新值,您只需更新模型:
Button selectAll = new Button("Select all");
selectAll.setOnAction(e -> {
for (AttributeRow row : attributeTable.getItems()) {
for (String group : groups) {
row.setActive(group, true);
}
}
});
这是一个 SSCCE:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class TableWithMappedBooleans extends Application {
private static final List<String> groups = Arrays.asList("Group 1", "Group 2", "Group 3", "Group 4");
@Override
public void start(Stage primaryStage) {
TableView<AttributeRow> attributeTable = new TableView<>();
attributeTable.setEditable(true);
TableColumn<AttributeRow, String> attributeColumn = new TableColumn<>("Attribute");
attributeColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
attributeTable.getColumns().add(attributeColumn);
for (String group : groups) {
TableColumn<AttributeRow, Boolean> groupColumn = new TableColumn<>(group);
groupColumn.setCellFactory(CheckBoxTableCell.forTableColumn(groupColumn));
groupColumn.setCellValueFactory(cellData -> cellData.getValue().activeProperty(group));
attributeTable.getColumns().add(groupColumn);
}
// generate data:
for (int i = 1 ; i <= 10; i++) {
AttributeRow row = new AttributeRow(groups);
row.setName("Attribute "+i);
attributeTable.getItems().add(row);
}
// button to select everything:
Button selectAll = new Button("Select all");
selectAll.setOnAction(e -> {
for (AttributeRow row : attributeTable.getItems()) {
for (String group : groups) {
row.setActive(group, true);
}
}
});
// for debugging, to check data are updated from check boxes:
Button dumpDataButton = new Button("Dump data");
dumpDataButton.setOnAction(e -> {
for (AttributeRow row : attributeTable.getItems()) {
String groupList = groups.stream()
.filter(group -> row.isActive(group))
.collect(Collectors.joining(", "));
System.out.println(row.getName() + " : " + groupList);
}
System.out.println();
});
HBox buttons = new HBox(5, selectAll, dumpDataButton);
buttons.setAlignment(Pos.CENTER);
buttons.setPadding(new Insets(5));
BorderPane root = new BorderPane(attributeTable, null, null, buttons, null);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}