Results 1 to 9 of 9

Thread: zip to JTree

  1. #1
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default zip to JTree

    Hello,

    Anybody know any method to explore a zip file and convert their content to a JTree?

    Thank you very much.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You mean represent the contents as a JTree rather than converting the zip file to a JTree, right?
    Start by reading the JTree tutorial.

  3. #3
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Thank you.

    But my problem is how to extract data from a zip file in a format that I can build a JTree or DefaultMutableTreeNode.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You do know How to read Zip files, right?

  5. #5
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Yes.

    But I can not find solution for my problem.

    The builders of a JTree are:

    JTree(TreeNode)
    JTree(TreeNode, boolean)
    JTree(TreeModel)
    JTree()
    JTree(Hashtable)
    JTree(Object[])
    JTree(Vector)

    But exploring a zip file I'm not able to create any of these structures.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Read the whole of the JTree tutorial that I posted above.

  7. #7
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Finally,

    I found a possible solution:

    public TreeNode generarArbre() {
    ZipFile file = null;
    try {
    file = new ZipFile(ruta);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    System.exit(1);
    }
    MutableTreeNode arrel = new DefaultMutableTreeNode(nomFitxer);
    Map<String,MutableTreeNode> nodes = new HashMap<String,MutableTreeNode>();
    nodes.put(nomFitxer, arrel);
    for (Enumeration<? extends ZipEntry> e = file.entries(); e.hasMoreElements();) {
    ZipEntry entry = e.nextElement();
    String name = entry.getName();
    MutableTreeNode node = new DefaultMutableTreeNode(name);
    nodes.put(name, node);

    MutableTreeNode parent = getParent(name, nodes);
    parent.insert(node, parent.getChildCount());
    }
    return arrel;
    }

    private MutableTreeNode getParent(String name, Map<String,MutableTreeNode> nodes) {
    File f = new File(name);
    String parent = f.getParent();
    // parent == null => parentNode is the root
    if (parent == null) {
    System.out.println("Nodes:"+nodes.get(nomFitxer));
    return nodes.get(nomFitxer);
    } else {
    parent = parent+File.separator;
    }
    MutableTreeNode parentNode = nodes.get(parent);
    // omitted: null check and creation
    return parentNode;
    }

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Your code contains a smiley inside it!
    Better use code tags when posting code tags next time.

    So, does it work?

  9. #9
    icsbcn is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

Similar Threads

  1. JCheckbox in Jtree
    By shajuantony in forum AWT / Swing
    Replies: 10
    Last Post: 09-09-2009, 10:42 AM
  2. Jtree with checkbox
    By msankar.ravi in forum AWT / Swing
    Replies: 1
    Last Post: 07-09-2009, 11:25 PM
  3. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  4. JTree Problem
    By ngpamela in forum XML
    Replies: 0
    Last Post: 12-10-2008, 04:10 PM
  5. A Simple JTree Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •