我正在根据评论修改我的问题。我有一个JavaFX TableView,其复选框列的数量仅在运行时已知。因此,要创建列,我会这样做:
dispatch_async(transitionQueue,^{
//heavy lifting code
viewerPVC=..... //this should block here otherwise will not work
dispatch_async(dispatch_get_main_queue(), ^{
[activity stopAnimating];
[self transitionToAnotherViewer:viewerPVC];
});
});
问题是,这个TableView的表模型怎么样?如果有一个固定数量的复选框列,比如2列,我的模型如下所示:
TableColumn attributeColumn = new TableColumn("Attribut");
attributeColumn.setCellValueFactory(new PropertyValueFactory<AttributeRow, String>("name"));
attributeTable.getColumns().add(attributeColumn);
for (String group : companyGroups)
{
TableColumn< AttributeRow, Boolean > groupColumn = new TableColumn<>( group );
groupColumn.setCellFactory(CheckBoxTableCell.forTableColumn(groupColumn));
groupColumn.setCellValueFactory( f -> f.getValue().activeProperty());
groupColumn.setEditable(true);
attributeTable.getColumns().add(groupColumn);
}
如果我有一个字符串列和一个复选框列,则此模型有效。但是我该怎么做,如果我有多个复选框列,其数字只在运行时知道?
答案 0 :(得分:1)
您还没有真正描述过数据的结构,但看起来有一些String
s(companyGroups
)的集合,每一行都是由表格表示的一个String
(name
)和一个布尔值,用于companyGroups
的每个元素。因此,实现此目的的一种方法就是在模型类Map<String, BooleanProperty>
中定义AttributeRow
,其中地图中的键旨在成为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);
}
}
列的单元格值工厂没有什么特别之处 - 它仍然只需将每一行映射到该列的相应observable属性:
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);
}
}