有人可以建议如何在JavaFX中动态地将TableColumn添加到TableView中吗?
我需要在tableView上显示类Row,但我知道另一个动作后的节点数。当我尝试使用Node中的StringProperty来设置CellValueFactory时,我遇到了麻烦。从Node类我需要只显示保留和可靠性。
public class Row {
private int step;
private Node[] nodes;
private double p = 1;
private double c;
private StringProperty stepProperty;
private StringProperty pProperty;
private StringProperty cProperty;
Row(int step, Node[] nodes, double c){
this.step = step;
this.nodes = nodes;
for (Node node:
nodes) {
p *= node.getReliability();
}
this.c = c;
}
public int getStep() {return step;}
public Node getNode(int index) {return nodes[index];}
public double getP() {return p;}
public double getC() {return c;}
public StringProperty stepProperty() {
this.stepProperty = new SimpleStringProperty(step + "");
return stepProperty;
}
public StringProperty pProperty() {
this.pProperty = new SimpleStringProperty(p + "");
return pProperty;
}
public StringProperty cProperty() {
this.cProperty = new SimpleStringProperty(c + "");
return cProperty;
} }
public class Node {
private StringProperty reserved;
private StringProperty reliability;
private StringProperty price;
Node(String reserved, String reliability, String price){
this.reserved = new SimpleStringProperty(reserved);
this.reliability = new SimpleStringProperty(reliability);
this.price = new SimpleStringProperty(price);
}
public StringProperty reliabilityProperty() {
return reliability;
}
public StringProperty priceProperty() {
return price;
}
public StringProperty reservedProperty() {
return reserved;
}
public double getReliability(){
return Double.parseDouble(reliability.get());}
public int getPrice(){return Integer.parseInt(price.get());}
public int getReserved(){return Integer.parseInt(reserved.get());}
}
答案 0 :(得分:0)
您只需要以下几行:
TableView<Row> table = new TableView<>();
int numNodes = ... ;
TableColumn<Row, Number> stepCol = new TableColumn<>("Step");
stepCol.setCellValueFactory(cellData -> cellData.getValue().stepProperty());
table.getColumn().add(stepCol);
for (int n = 0 ; n < numNodes ; n++) {
final int nodeIndex = n ;
TableColumn<Row, String> reservedCol = new TableColumn<>("Reserved - "+nodeIndex);
reservedCol.setCellValueFactory(cellData -> cellData.getValue().getNode(nodeIndex).reservedProperty());
TableColumn<Row, String> reliabilityCol = new TableColumn<>("Reliability - "+nodeIndex);
reliabilityCol.setCellValueFactory(cellData -> cellData.getValue().getNode(nodeIndex).reliabilityProperty());
table.getColumns().add(reservedCol);
table.getColumns().add(reliabilityCol);
}
TableColumn<Row, String> pCol = new TableColumn<>("P");
stepCol.setCellValueFactory(cellData -> cellData.getValue().pProperty());
table.getColumn().add(pCol);
TableColumn<Row, String> cCol = new TableColumn<>("C");
stepCol.setCellValueFactory(cellData -> cellData.getValue().cProperty());
table.getColumn().add(cCol);