Hi, my two cents

package example;

import java.net.URL;
import org.apache.pivot.collections.*;
import org.apache.pivot.util.ThreadUtilities;
import org.apache.pivot.wtk.*;
import org.apache.pivot.wtk.content.*;
import org.apache.pivot.wtk.media.*;

public class TreeViewExample implements Application {

    private Window window = null;

    public void startup(Display display, Map<String, String> properties)
throws Exception {
        window = new Window();
        //images
        Image imgBranch = loadImage("example/branch.png");
        Image imgLeaf = loadImage("example/leaf.png");
        //nodes
        TreeBranch branch1 = new TreeBranch(imgBranch, "Branch 1");
        TreeBranch branch2 = new TreeBranch("Branch 2");
        TreeBranch branch3 = new TreeBranch(imgBranch, "Branch 3");
        TreeNode leaf1 = new TreeNode(imgLeaf, "leaf 1");
        TreeNode leaf2 = new TreeNode("leaf 2");
        TreeNode leaf3 = new TreeNode(imgLeaf, "leaf 3");
        branch1.add(branch2);
        branch2.add(leaf1);
        branch2.add(leaf2);
        ArrayList<TreeNode> nodes = new ArrayList<TreeNode>();
        nodes.add(branch1);
        nodes.add(branch3);
        nodes.add(leaf3);

        TreeView treeView = new TreeView(nodes);
        window.setContent(treeView);
        window.setMaximized(true);

        window.open(display);
    }

    private Image loadImage(String path) {
        Image result = null;
        try {
            URL url = ThreadUtilities.getClassLoader().getResource(path);
            result = Picture.load(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public void suspend() throws Exception {
    }

    public void resume() throws Exception {
    }

    public boolean shutdown(boolean optional) throws Exception {
        if (window != null) {
            window.close();
        }
        return false;
    }

    public static void main(String[] args) {
        DesktopApplicationContext.main(TreeViewExample.class, args);
    }
}

Cheers,

-Alejandro

-----Original Message-----
From: Greg Brown [mailto:[email protected]] 
Sent: Lunes, 19 de Abril de 2010 06:33 p.m.
To: [email protected]
Subject: Re: TreeView

You can simply create instances of org.apache.pivot.wtk.content.TreeBranch
and TreeNode programmatically rather than declaring them in WTKX. If you
haven't seen it, you may find the WTKX Primer handy for understanding how
WTKX maps to Java:

  http://pivot.apache.org/tutorials/wtkx-primer.html

Here is an example that dynamically creates tree data:

 
http://svn.apache.org/repos/asf/pivot/trunk/tools/src/org/apache/pivot/tools
/json/JSONViewer.java

Hope this helps,
Greg

On Apr 19, 2010, at 6:10 PM, Patrick Shea wrote:

> Hi, I'm trying to dynamically populate a treeview but I don't see any
examples on how to do this, I only see xml based examples.
> 
> Does anybody have a code snippet of programatically building a treeview?
> 
> Thanks
> Patrick
> 

Reply via email to