2016-06-03 4 views
-1

누군가 JavaFX에서 TableColumn을 동적으로 TableView에 추가하는 방법을 제안 할 수 있습니까?TableFolumn을 JavaFX의 TableView에 동적으로 추가하는 방법은 무엇입니까?

Row 클래스를 tableView에 표시해야하지만 다른 작업을 수행 한 후 노드 수를 알고 있습니다. 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());} 
} 
+1

당신이 같은 테이블보고 원하는 것을 설명 할 수 있습니까? 그것은 분명하지 않습니다. –

+0

예를 들어, 내가 2 노드를 가지고 있다면 tableView에 표시해야합니다. Row.Step | 노드 [0] .reserved | 노드 [0]. 신뢰성 | 노드 [1] .reserved | 노드 [2]. 신뢰성 | Row.p | Row.c –

+0

좋습니다. 도움이됩니다. 모든'Row' 객체는 포함하고있는 배열에 같은 수의 노드를 가지고 있습니까? 서로 다른'Row' 객체의 노드 수가 다른 경우 어떻게됩니까? 테이블이 표시되는 동안 새로운'Row' 객체를 추가합니까? 'Row' 객체의 노드 수는 언제 알 수 있습니까? –

답변

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

정말 고마워요 –

관련 문제