2010-03-22 3 views
1

Java에서 매우 큰 TreeView 컨트롤이있는 응용 프로그램이 있습니다. 나뭇잎의 XPath와 같은 요소 만 목록에있는 트리 컨트롤의 내용 (JList가 아닌 문자열)을 가져 오려고합니다.다른 응용 프로그램의 트리보기 내용 도용

 
/Item1/Item1.1/Item1.1.1 
/Item1/Item1.2 
/Item2/Item2.1 

내가 어떤 소스 코드 나 같은 편리한 아무것도하지 않는 예를 들면 다음과 같습니다 루트

 
|-Item1 
    |-Item1.1 
    |-Item1.1.1 (leaf) 
    |-Item1.2 (leaf) 
|-Item2 
    |-Item2.1 (leaf) 

겠습니까 출력입니다. Window 항목 자체를 파고이 데이터를 추출하는 데 사용할 수있는 도구가 있습니까? 손으로 입력하는 것이 나의 유일한 다른 옵션이기 때문에 몇 가지 사후 처리 단계가 있다면 상관 없습니다.

+2

JTree가 있다고 하시겠습니까? – Wintermut3

+0

나는이 질문에 대한 두 가지 해석을 내놓았으며 그 두 가지 모두에 답하려고했다. 잘만되면 적어도 한 가지 대답이 도움이되기를 바랍니다. :-) –

+0

응용 프로그램에 JTree가 있다고 가정하지만 직접 액세스 할 수는 없습니다. 이것은 Windows 메시지 또는 그와 같은 추악한 것이 필요할 수 있습니다. – User1

답변

1

우리는 당신이 TreeModel (당신이 JTree.getModel()를 사용하여 JTree에서 얻을 수있는), 다음 코드는 "/"에있는 나무의 잎을 인쇄 할 수 있다고 가정하는 경우 - 당신이 찾고있는 구분 된 형식 : 물론

/** 
* Prints the path to each leaf in the given tree to the console as a 
* "/"-separated string. 
* 
* @param tree 
*   the tree to print 
*/ 
private void printTreeLeaves(TreeModel tree) { 
    printTreeLeavesRecursive(tree, tree.getRoot(), new LinkedList<Object>()); 
} 

/** 
* Prints the path to each leaf in the given subtree of the given tree to 
* the console as a "/"-separated string. 
* 
* @param tree 
*   the tree that is being printed 
* @param node 
*   the root of the subtree to print 
* @param path 
*   the path to the given node 
*/ 
private void printTreeLeavesRecursive(TreeModel tree, 
             Object node, 
             List<Object> path) { 
    if (tree.getChildCount(node) == 0) { 
     for (final Object pathEntry : path) { 
      System.out.print("/"); 
      System.out.print(pathEntry); 
     } 
     System.out.print("/"); 
     System.out.println(node); 
    } 
    else { 
     for (int i = 0; i < tree.getChildCount(node); i++) { 
      final List<Object> nodePath = new LinkedList<Object>(path); 
      nodePath.add(node); 
      printTreeLeavesRecursive(tree, 
            tree.getChild(node, i), 
            nodePath); 
     } 
    } 
} 

, 당신은 단지 콘솔 트리의 내용을 인쇄하지 않은 경우, 당신은 그러한 파일로 출력 또는 서면으로 또는에 추가로 다른 뭔가로 println 문을 대체 할 수 이 메서드에 추가 인수로 전달되는 Writer 또는 StringBuilder

1

당신은 이미 당신이 JTree이되면 무엇을 알고있는 경우

(나는 ... 질문의 해석에 따라, 두 번째 대답을 게시하도록하겠습니다)와 방금 JTree을 찾기 위해 노력하고

/** 
* Searches the component hierarchy of the given container and returns the 
* first {@link javax.swing.JTree} that it finds. 
* 
* @param toSearch 
*   the container to search 
* @return the first tree found under the given container, or <code>null</code> 
*   if no {@link javax.swing.JTree} could be found 
*/ 
private JTree findTreeInContainer(Container toSearch) { 
    if (toSearch instanceof JTree) { 
     return (JTree)toSearch; 
    } 
    else { 
     for (final Component child : toSearch.getComponents()) { 
      if (child instanceof Container) { 
       JTree result = findTreeInContainer((Container)child); 
       if (result != null) { 
        return result; 
       } 
      } 
     } 
     return null; 
    } 
} 
: 임의 Container (더 JTree가 발견되지 않는 경우 나 null), 다음 코드는 지정된 Container를 검색하여 상기 제 찾은 JTree를 반환한다 (어떤 JComponent, Window, JFrame 등을 포함)에있는 구성 요소