Java Question

How to dynamicly add TableColumn to TableView in JavaFX

can someone suggest how to dynamicly add TableColumn to TableView in JavaFX.

I need to display class Row at tableView but I know count of Nodes after another action. I've got trouble when I tried to use StringProperty from Node to set CellValueFactory. From Node class I need to display only reserved and reliability.

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());}
}

Answer

You just need something along the following lines:

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);