2016-06-28 3 views
1

네트워크 장치를 표시하는 트리가 있습니다. 트리가 필터링되어 장치가 위치 및 장치 유형별로 표시됩니다. 예를 들어 노드 "Office"에는 하위 노드 인 "Computer"와 "Printer"가 있고 그 아래에 장치가 있습니다.SelectionPaths []에 자식 노드 추가

위치 노드를 선택할 때 모든 노드를 선택하고 내 나무의 selectionPaths []에 추가하여 모두 강조 표시하고 싶습니다. 아래는 내 코드이지만,이 코드를 작동시키는 방법을 알 수는 없습니다.

private class DefaultDeviceTreeCellRenderer extends DefaultTreeCellRenderer { 

    private JLabel label; 

    DefaultDeviceTreeCellRenderer() { 
     super(); 
     label = new JLabel(); 
    } 

    @Override 
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, 
      boolean leaf, int row, boolean hasFocus) { 
     DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; 
     Object o = node.getUserObject(); 
     if (o instanceof DefaultDevice) { 
      DefaultDevice defaultDevice = (DefaultDevice) o; 
      label.setIcon(defaultDevice.getIcon()); 
      label.setText(defaultDevice.toString()); 
     } else { 
      System.out.println("Cell Class= " + o.getClass()); 
      //Set all children of this Component as selected. 
      label.setIcon(null); 
      label.setText("" + value); 
      TreePath[] selectionPaths = tree.getSelectionPaths(); 
      if (selectionPaths == null) { 
       System.out.println("Nothing Selected. "); 
      } else { 
       //keep current selection path 
       //if there are leaf nodes, add their selection paths. 
       int i = 0; 
       while (i < node.getChildCount()) { 
        //add node.getNextLeaf().getPath() to selection Paths 
        ArrayList<TreePath> arrayList = new ArrayList<>(Arrays.asList(selectionPaths)); 
        arrayList.add(new TreePath(((DefaultMutableTreeNode)node.getChildAt(i)).getPath())); 
        TreePath[] toArray = arrayList.toArray(new TreePath[arrayList.size()]); 
        this.printSelectionPath(selectionPaths); 
        this.printSelectionPath(toArray); 
        // tree.setSelectionPaths(toArray); 
        i++; 
       } 
      } 
      // System.out.println("Selection Paths.size="+selectionPaths.length); 
      // ArrayList arrayList = new ArrayList(Arrays.asList(selectionPaths)); 
      // System.out.println("ChildNode Path=" + node.getPath()[a]); 

     } 
     label.setOpaque(true); 
     if (selected) { 

      label.setBackground(this.backgroundSelectionColor); 
      label.setForeground(this.textSelectionColor); 
     } else { 
      label.setBackground(this.backgroundNonSelectionColor); 
      label.setForeground(this.textNonSelectionColor); 
     } 
     return label; 
    } 

    private void printSelectionPath(TreePath[] selectionPaths) { 
     System.out.println("\nTreePath:"); 
     for (int i = 0; i < selectionPaths.length; i++) { 
      System.out.println("Selection Path= " + selectionPaths[i]); 
      //TreePath treePath = new TreePath(node.getPath()); 
      // System.out.println("TreePath= " + treePath); 
     } 
} 
+0

는 : http://docs.oracle.com/javase/7/docs/api/javax/swing/tree/TreeSelectionModel.html – ControlAltDel

+0

당신은 것 예를 제공 할 수 있습니다 이것을 성취합니까? –

+0

'그 아래의 모든 노드를 선택하고 내 tree의 selectionPaths []에 추가하여 모두 강조 표시되도록하십시오.'SelectionModel을 변경하여보기를 변경 하시겠습니까? 상위 '위치'노드가 있는지 여부에 따라보기를 변경하는 것이 더 쉽고 선택됩니다. – copeg

답변

0

이 작업은 DefaultTreeSelectionModel을 확장하고 몇 가지 방법을 수정하여 수행했습니다. 다음은 확장 클래스입니다.

public static class MyTreeSelectionModel extends DefaultTreeSelectionModel { 

    public MyTreeSelectionModel() { 
     super(); 
    } 

    @Override 
    public void removeSelectionPath(TreePath path) { 
     //remove path and its children 
     Object lastPathComponent = path.getLastPathComponent(); 
     ArrayList<TreePath> childPaths = new ArrayList(); 
     if (lastPathComponent instanceof DefaultMutableTreeNode) { 
      DefaultMutableTreeNode dn = (DefaultMutableTreeNode) lastPathComponent; 
      this.getDecendents(dn, childPaths); 
     } else { 
      System.out.println("Not a DefaultMutableTreeNode"); 
     } 
     childPaths.add(path); 
     this.removeSelectionPaths(childPaths.toArray(new TreePath[childPaths.size()])); 
    } 

    @Override 
    public void addSelectionPath(TreePath path) { 
     //add the path and its children 
     Object lastPathComponent = path.getLastPathComponent(); 
     ArrayList<TreePath> childPaths = new ArrayList(); 
     if (lastPathComponent instanceof DefaultMutableTreeNode) { 
      DefaultMutableTreeNode dn = (DefaultMutableTreeNode) lastPathComponent; 
      System.out.println("Last Path Component= " + dn); 
      //get decendents 
      this.getDecendents(dn, childPaths); 
     } else { 
      System.out.println("Not a DefaultMutableTreeNode"); 
     } 
     childPaths.add(path); 
     this.addSelectionPaths(childPaths.toArray(new TreePath[childPaths.size()])); 
     // super.addSelectionPath(path); //To change body of generated methods, choose Tools | Templates. 
    } 

    private void getDecendents(DefaultMutableTreeNode dn, ArrayList<TreePath> childPaths) { 
     Enumeration children = dn.children(); 
     if (children != null) { 
      while (children.hasMoreElements()) { 
       DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement(); 
       System.out.println("Adding " + childNode); 
       TreeNode[] path1 = childNode.getPath(); 
       childPaths.add(new TreePath(path1)); 
       getDecendents(childNode, childPaths); 
      } 
     } 

    } 

} 
당신은 정말 정의하는 TreeSelectionModel를 작성해야