2015-03-18 51 views
-1

請考慮我有一個treetable,其中某些行的某些列與某些列的名稱相同。我怎樣才能找出那些細胞併爲它們設定一些價值。下面的代碼寫在所有單元格中,但是由於某些行名稱(myItem)等於列名稱(標籤),我希望在某些單元格中看到「是」。 假設第3列的名稱爲Org,並且第2行的項目具有相同的名稱。所以我期望在單元格2 X 3看到單詞yes。 起初,我創建了第一列,這是一棵樹。然後我插入所有其他列。在TreeTableView中找到具有相同名稱的行和列的交集,JavaFX

private TreeTableView<String> drawTable() { 

    root.setExpanded(true); 

    Set<String> funcAllKeys = new HashSet<>(dc.getSortedfuncAll().keySet()); 
    funcAllKeys.removeAll(dc.getCombiFunc().keySet()); 
    for (List<String> value : dc.getCombiFunc().values()) { 
     funcAllKeys.removeAll(value); 
    } 
    for (String valueremained : funcAllKeys) { 
     ArrayList<String> tempNameId = new ArrayList<>(); 
     tempNameId.add(dc.getSortedfuncAll().get(valueremained)); 
     // all elements which are not in combined functions (They are all 
     // orphan) 
     root.getChildren().add(new TreeItem<String>(tempNameId.get(0))); 
    } 


    // ////////////////treetable//////////////////////////// 
    /** 
    * makes treeTable and set the nodes to it. 
    * */ 

    treeTable.setRoot(root); 
    Arrays.stream(dc.getRootKeys()).forEach(
      rootKey -> root.getChildren().add(
        createTreeItem(dc.getCombiFunc(), rootKey))); 

    // ////////////////First column///////////////////////// 
    /** 
    * creates first column with no caption and sets treeview for that. 
    * */ 
    TreeTableColumn<String, String> firstColumn = new TreeTableColumn<>(""); 
    treeTable.getColumns().add(firstColumn);// Tree column 
    firstColumn 
      .setCellValueFactory(new Callback<CellDataFeatures<String, String>, ObservableValue<String>>() { 
       public ObservableValue<String> call(
         CellDataFeatures<String, String> p) { 
        return new ReadOnlyStringWrapper(p.getValue() 
          .getValue()); 
       } 
      }); 
    TreeMap<String, List<TreeItem<String>>> parentChild = new TreeMap<>(); 
    for (TreeItem node : root.getChildren()) { 
     parentChild.put(node.getValue().toString(), node.getChildren()); 
    } 

    // //////////////////Rest Columns//////////////////////// 
    /** 
    * go through all organizations which have a function and make column 
    */ 
    for (Entry<String, String> ent : dc.getSortedAssignedOrg().entrySet()) { 

     TreeTableColumn<String, ArrayList<String>> col = new TreeTableColumn<>(); 
     Label label = new Label(ent.getValue()); 
     col.setGraphic(label); 

     TreeMap<String, List<String>> temp = (TreeMap<String, List<String>>) dc 
       .getFuncTypeOrg().clone();       


      List<String> list = temp.firstEntry().getValue(); 
      String key = temp.firstEntry().getKey(); 


        col.setCellFactory(new Callback<TreeTableColumn<String, ArrayList<String>>, TreeTableCell<String, ArrayList<String>>>(){ 

         @Override 
         public TreeTableCell<String, ArrayList<String>> call(
           TreeTableColumn<String, ArrayList<String>> param) { 

          return new TreeTableCell<String, ArrayList<String>>() { 
           public void updateItem(ArrayList<String> item, 
             boolean empty) { 
            for (Entry<String, List<TreeItem<String>>> entp : parentChild 
              .entrySet()) { 
             for (TreeItem myItem : entp.getValue()) { 

            if ((myItem.getValue().equals(label.getText()) { 
             setText("yes"); 
            } 
            else setText("No"); 

           } 
            } 
           } 



          }; 
         } 

        }); 


     treeTable.getColumns().add(col); 
    } 

    treeTable.setPrefWidth(1200); 
    treeTable.setPrefHeight(500); 
    treeTable.setShowRoot(false); 
    treeTable.setEditable(false); 

    treeTable.setTableMenuButtonVisible(true); 

    return treeTable; 
    } 



    private TreeItem<String> createTreeItem(TreeMap<String, List<String>> data, 
     String rootKey) { 
    TreeItem<String> item = new TreeItem<>(); 
    item.setValue(rootKey); 
    item.setExpanded(true); 

    List<String> childData = data.get(rootKey); 
    if (childData != null) { 
     childData.stream().map(child -> createTreeItem(data, child)) 
       .collect(Collectors.toCollection(item::getChildren)); 
    } 
    String valueName = item.getValue(); 
    item.setValue((dc.getSortedfuncAll().get(valueName))); 
    return item; 
} 

} 
+0

你的問題(和代碼)是非常不清楚的(對我來說,至少)。從頭開始創建一個簡單的[MCVE](http://stackoverflow.com/help/mcve)可能會更容易,它清楚地顯示了您正在嘗試執行的操作。但無論如何,你有'TreeItem myItem',而'label.getText()'返回一個'String',所以'myItem.equals(label.getText())'永遠不可能是'true'。 – 2015-03-18 22:46:29

+0

是的,你是對的。我現在添加.getValue()myItem。它打印出字符串。但是,列和行的碰撞點仍然沒有填充單詞yes – Iman 2015-03-18 22:55:33

+1

好吧,就我所知,您也一直在同一列上重複設置單元工廠(多次,多次)。每次調用'setCellFactory'都會取代之前調用的單元工廠,因此大部分代碼都是多餘的。但是隻有最外層循環中最後一個值爲「i」的單元工廠和中間循環中最後一個「entp」的值以及最內層循環中最後一個「myItem」的值纔是使用的單元工廠。但我真的不知道你在這裏做什麼。你需要清理代碼,這是可以理解的。 – 2015-03-18 23:03:12

回答

1

我與this.getTreeTableRow()每個單元有行名稱。getItem()時,可能成功地將其與列名比較。這裏是細胞工廠:

col.setCellFactory(new Callback<TreeTableColumn<String, ArrayList<String>>, TreeTableCell<String, ArrayList<String>>>() { 
       @Override 
       public TreeTableCell<String, ArrayList<String>> call(
         TreeTableColumn<String, ArrayList<String>> param) { 
        return new TreeTableCell<String, ArrayList<String>>() { 
         private List<Double> result = new ArrayList<Double>(); 
         private List<Double> weight = new ArrayList<>(); 
         private double lastresult = new Double(0); 

         public void updateItem(ArrayList<String> item, 
           boolean empty) { 

          super.updateItem(item, empty); 



           if (label.getText().equals(
             this.getTreeTableRow().getItem()) 
             { 
            setText("yes"); 
           } 
          } 

         } 

        }; 
       }; 

      });// end cell factory