Anyone have a free tree for me? (Swing)

Discussion in 'Java' started by timasmith@hotmail.com, Mar 24, 2006.

  1. Guest

    Hi,

    I have a done some somewhat average tree code and while functional it
    is not particularly satisfying. Are there any free Swing components
    which includes an advanced JTree.

    When I say advanced I mean that out the box it provides

    a) seperation of events (on select, right click etc.) from the tree
    b) icon management
    c) dynamic expansion
    d) drag and drop
    etc.

    Essentially I want to interact something like this...

    YourTree tree = new YourTree()
    tree.addOnClick(addMyListener()...);
    tree.setLeafIcon(myIcon);
    tree.addOnChildrenRequest(addMyListener()...)
    tree.addOnSelection(addMyListener()...)
    tree.addOnDragandDrop(addMyListener()...)

    etc.

    Anyone seen such a marvelous thing?

    thanks

    Tim
     
    , Mar 24, 2006
    #1
    1. Advertising

  2. I've been doing swing programming for a long time, and everything you
    ask for is functionally available. First you should check out the "how
    to use jtree" link in the javadocs for JTree.

    The easiest thing for you would probably be to use the
    DefaultMutableTreeNode with a specific userobject. The userobject would
    be a class which kept track of each of the settings for your tree node.
    You could then write all your selection listener, expansion listener,
    and tree renderer to work of the info in the user object.

    good luck.

    -rich

    wrote:
    > Hi,
    >
    > I have a done some somewhat average tree code and while functional it
    > is not particularly satisfying. Are there any free Swing components
    > which includes an advanced JTree.
    >
    > When I say advanced I mean that out the box it provides
    >
    > a) seperation of events (on select, right click etc.) from the tree
    > b) icon management
    > c) dynamic expansion
    > d) drag and drop
    > etc.
    >
    > Essentially I want to interact something like this...
    >
    > YourTree tree = new YourTree()
    > tree.addOnClick(addMyListener()...);
    > tree.setLeafIcon(myIcon);
    > tree.addOnChildrenRequest(addMyListener()...)
    > tree.addOnSelection(addMyListener()...)
    > tree.addOnDragandDrop(addMyListener()...)
    >
    > etc.
    >
    > Anyone seen such a marvelous thing?
    >
    > thanks
    >
    > Tim
    >
     
    Richard Huddleston, Mar 24, 2006
    #2
    1. Advertising

  3. Aman Guest

    Hi

    You can go thru the following code and i think it will be able to help
    you

    // *****************************************************************
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;

    import java.awt.LayoutManager;

    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.event.*;

    public class DirTree extends JFrame
    {
    public static final String APP_NAME = "DIRECTORIES TREE";
    public static final ImageIcon ICON_FOLDER = new
    ImageIcon("Computer.gif");
    public static final ImageIcon ICON_DISK = new
    ImageIcon("disk.gif");
    public static final ImageIcon ICON_FOLDERCOLLAPSE = new
    ImageIcon("folder.gif");
    public static final ImageIcon ICON_EXPANDEDFOLDER = new
    ImageIcon("expandedfolder.gif");

    protected JTree m_tree;
    protected DefaultTreeModel m_model;
    protected JTextField m_display;

    //Container getContentPane().setLayout(new FlowLayout());

    public DirTree()
    {
    super(APP_NAME);
    setSize(400, 300);
    DefaultMutableTreeNode top = new DefaultMutableTreeNode(new
    IconData(ICON_FOLDER, null, "Computer"));

    DefaultMutableTreeNode node;
    File[] roots = File.listRoots();
    for (int k = 0; k < roots.length; k++) {
    node=new DefaultMutableTreeNode(new IconData(ICON_DISK, null, new
    FileNode(roots[k])));
    top.add(node);
    node.add(new DefaultMutableTreeNode(new Boolean(true)));
    }
    m_model = new DefaultTreeModel(top);
    m_tree = new JTree(m_model);

    m_tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    m_tree.putClientProperty("JTree.lineStyle","Angled");
    TreeCellRenderer renderer = new IconCellRenderer();
    m_tree.setCellRenderer(renderer);
    DirExpansionListener dListner = new DirExpansionListener();
    m_tree.addTreeExpansionListener(dListner);
    // dListner.setRefLabelObject(this.);
    m_tree.setShowsRootHandles(true);
    m_tree.setEditable(false);

    JScrollPane s=new JScrollPane();
    s.getViewport().add(m_tree);
    getContentPane().add(s, BorderLayout.CENTER);

    m_display=new JTextField();
    m_display.setEditable(false);
    getContentPane().add(m_display, BorderLayout.NORTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    }
    DefaultMutableTreeNode getTreeNode (TreePath path)
    {
    return (DefaultMutableTreeNode) (path.getLastPathComponent());
    }
    FileNode getFileNode(DefaultMutableTreeNode node)
    {
    if(node==null)
    return null;
    Object obj = node.getUserObject();
    if (obj instanceof IconData)
    obj = ((IconData)obj).getObject();
    if (obj instanceof FileNode)
    return (FileNode)obj;
    else
    return null;
    }

    //Make sure expansion is threaded and updating the tree model
    //only occurs within the event dispatching thread.
    class DirExpansionListener implements TreeExpansionListener

    {
    private Object objRefTLabel;

    public void setRefLabelObject(Object refLabel){
    this.objRefTLabel = refLabel;
    }

    public void treeExpanded(TreeExpansionEvent event) {
    final DefaultMutableTreeNode
    node=getTreeNode(event.getPath());
    final FileNode fnode = getFileNode(node);

    Thread runner = new Thread()
    {
    public void run()
    {
    if (fnode !=null && fnode.expand(node))
    {
    Runnable runnable=new Runnable()
    {
    public void run()
    {
    m_model.reload(node);
    }
    };
    SwingUtilities.invokeLater(runnable);
    }
    }
    };
    runner.start();
    }
    public void treeCollapsed(TreeExpansionEvent event)
    {}
    }


    class DirSelectionListener implements TreeSelectionListener
    {
    public void valueChanged(TreeSelectionEvent event)
    {
    DefaultMutableTreeNode node=getTreeNode(event.getPath());
    FileNode fnode=getFileNode(node);
    if (fnode != null)
    m_display.setText(fnode.getFile().getAbsolutePath());
    else
    m_display.setText("Nothing Selected");
    }
    }
    public static void main(String argv[])
    {
    new DirTree();
    }
    }

    class IconCellRenderer extends JLabel implements TreeCellRenderer
    {
    protected Color m_textSelectionColor;
    protected Color m_textNonSelectionColor;
    protected Color m_bkSelectionColor;
    protected Color m_bkNonSelectionColor;
    protected Color m_borderSelectionColor;

    protected boolean m_selected;

    public IconCellRenderer()
    {
    super();
    m_textSelectionColor = UIManager.getColor(
    "Tree.selectionForegroung");
    m_textNonSelectionColor=UIManager.getColor("Tree.textForeground");
    m_bkSelectionColor=UIManager.getColor("Tree.selectionBackground");
    m_bkNonSelectionColor=UIManager.getColor("Tree.textBackground");
    m_borderSelectionColor =
    UIManager.getColor("Tree.selectionBorderColor");
    setOpaque(false);
    }

    public Component getTreeCellRendererComponent(JTree tree, Object
    value, boolean sel, boolean expanded, boolean leaf, int row, boolean
    hasFocus)
    {
    DefaultMutableTreeNode node=(DefaultMutableTreeNode)value;
    Object obj=node.getUserObject();
    setText(obj.toString());

    // if (obj instanceof IconData)
    // setText("Retrievingdata...");

    if(obj instanceof IconData)
    {
    IconData idata=(IconData)obj;
    if (expanded)
    setIcon(idata.getExpandedIcon());
    else
    setIcon(idata.getIcon());
    }
    else
    setIcon(null);

    setFont(tree.getFont());
    setForeground(sel ? m_textSelectionColor :
    m_textNonSelectionColor);
    setBackground(sel ? m_bkSelectionColor : m_bkNonSelectionColor);
    m_selected=sel;
    return this;
    }

    public void paintComponent (Graphics g)
    {
    Color bColor = getBackground();
    Icon icon= getIcon();

    g.setColor(bColor);
    int offset = 0;
    if(icon !=null && getText() != null)
    offset = (icon.getIconWidth()+getIconTextGap());
    g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);

    if (m_selected)
    {
    g.setColor(m_borderSelectionColor);
    g.drawRect(offset, 0, getWidth()-1-offset, getHeight()-1);
    }
    super.paintComponent(g);
    }
    }

    class IconData
    {
    protected Icon m_icon;
    protected Icon m_expandedIcon;
    protected Object m_data;

    public IconData (Icon icon, Object data)
    {
    m_icon = icon;
    m_expandedIcon = null;
    m_data = data;
    }

    public IconData(Icon icon, Icon expandedIcon, Object data)
    {
    m_icon=icon;
    m_expandedIcon=expandedIcon;
    m_data=data;
    }

    public Icon getIcon()
    {
    return m_icon;
    }

    public Icon getExpandedIcon()
    {
    return m_expandedIcon!=null ? m_expandedIcon : m_icon;
    }

    public Object getObject()
    {
    return m_data;
    }

    public String toString()
    {
    return m_data.toString();
    }
    }

    class FileNode
    {
    protected File m_file;

    public FileNode(File file)
    {
    m_file = file;
    }

    public File getFile()
    {
    return m_file;
    }

    public String toString()
    {
    return m_file.getName().length() > 0 ? m_file.getName() :
    m_file.getPath();
    }

    public boolean expand(DefaultMutableTreeNode parent)
    {
    DefaultMutableTreeNode flag =
    (DefaultMutableTreeNode)parent.getFirstChild();
    if(flag==null) //No flag
    return false;
    Object obj = flag.getUserObject();
    if (!(obj instanceof Boolean))
    return false; //Already expanded

    parent.removeAllChildren(); //Remove flag

    File[] files = listFiles();
    if (files == null)
    return true;

    Vector v=new Vector();
    for (int k=0; k<files.length; k++)
    {
    File f=files[k];
    if (!(f.isDirectory()))
    continue;

    FileNode newNode = new FileNode(f);

    boolean isAdded = false;
    for (int i=0; i<v.size();i++)
    {
    FileNode nd=(FileNode)v.elementAt(i);
    if (newNode.compareTo(nd) < 0)
    {
    v.insertElementAt(newNode, i);
    isAdded = true;
    break;
    }
    }
    if (!isAdded)
    v.addElement(newNode);
    }

    for (int i=0; i<v.size();i++)
    {
    FileNode nd = (FileNode)v.elementAt(i);
    IconData idata = new
    IconData(DirTree.ICON_FOLDERCOLLAPSE, DirTree.ICON_EXPANDEDFOLDER, nd);
    DefaultMutableTreeNode node = new
    DefaultMutableTreeNode(idata);
    parent.add(node);

    if (nd.hasSubDirs())
    node.add(new DefaultMutableTreeNode(new
    Boolean(true)));

    }
    return true;
    }


    public boolean hasSubDirs()
    {
    File[] files= listFiles();
    if (files == null)
    return false;
    for (int k=0; k<files.length; k++)
    {
    if (files[k].isDirectory())
    return true;
    }
    return false;
    }


    public int compareTo(FileNode toCompare)
    {
    return
    m_file.getName().compareToIgnoreCase(toCompare.m_file.getName());
    }

    protected File[] listFiles()
    {
    if (!m_file.isDirectory())
    return null;

    try {
    return m_file.listFiles();
    }
    catch (Exception ex)
    {
    JOptionPane.showMessageDialog(null, "Error reading
    directory "+m_file.getAbsolutePath(), "Warning",
    JOptionPane.WARNING_MESSAGE);
    return null;
    }
    }
    }
    ***********************************************************************

    even following code also can help you

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.event.*;

    public class Tree1 extends JFrame
    {
    protected JTree m_tree=null;
    protected DefaultTreeModel m_model = null;
    protected JTextField m_display;

    public Tree1()
    {
    super("SampleTree [OID]");
    setSize (400,300);

    Object[] nodes = new Object[5];
    DefaultMutableTreeNode top = new DefaultMutableTreeNode(new
    OidNode(1, "ISO"));
    DefaultMutableTreeNode parent = top;
    nodes[0] =top;

    DefaultMutableTreeNode node=new DefaultMutableTreeNode(new
    OidNode(0,"Standard"));
    parent.add(node);
    node=new DefaultMutableTreeNode(new OidNode(2,"Member-body"));
    parent.add(node);
    node=new DefaultMutableTreeNode(new OidNode(3,"Org"));
    parent.add(node);
    parent=node;
    nodes[1]=parent;

    node=new DefaultMutableTreeNode(new OidNode(6, "dod"));
    parent.add(node);
    parent=node;
    nodes[2]=parent;

    node = new DefaultMutableTreeNode(new OidNode(1,"Internet"));
    parent.add(node);
    parent=node;
    nodes[3]=parent;

    node = new DefaultMutableTreeNode(new OidNode(1,"Directory"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(2,"Management"));
    parent.add(node);
    nodes[4]=node;
    node.add(new DefaultMutableTreeNode(new OidNode(1,"Mib-2")));
    node=new DefaultMutableTreeNode(new OidNode(3,"Experimental"));
    parent.add(node);
    node=new DefaultMutableTreeNode(new OidNode(5,"Security"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(6,"snmpv2"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(7,"Mail"));
    parent.add(node);
    m_model=new DefaultTreeModel(top);
    m_tree = new JTree(m_model);

    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
    renderer.setOpenIcon(new ImageIcon("folderopen.gif"));
    renderer.setClosedIcon(new ImageIcon("folder.gif"));
    renderer.setLeafIcon(new ImageIcon("file.gif"));
    m_tree.setCellRenderer(renderer);
    m_tree.setShowsRootHandles(true);
    m_tree.setEditable(false);
    TreePath path=new TreePath(nodes);
    m_tree.setSelectionPath(path);

    m_tree.addTreeSelectionListener(new OidSelectionListener());

    JScrollPane s=new JScrollPane(m_tree);
    getContentPane().add(s, BorderLayout.CENTER);

    m_display=new JTextField();
    m_display.setEditable(false);
    getContentPane().add(m_display, BorderLayout.SOUTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    }

    public static void main(String argv[])
    {
    new Tree1();
    }

    class OidSelectionListener implements TreeSelectionListener
    {
    public void valueChanged(TreeSelectionEvent e)
    {
    TreePath path=e.getPath();
    Object[] nodes = path.getPath();
    String oid="";
    for (int k=0; k<nodes.length; k++)
    {
    DefaultMutableTreeNode node=(DefaultMutableTreeNode)nodes[k];
    OidNode nd=(OidNode)node.getUserObject();
    oid +="."+nd.getId();
    }
    m_display.setText(oid);
    }
    }
    }

    class OidNode
    {
    protected int m_id;
    protected String m_name;

    public OidNode(int id, String name)
    {
    m_id = id;
    m_name=name;
    }

    public int getId()
    {
    return m_id;
    }

    public String getName()
    {
    return m_name;
    }

    public String toString()
    {
    return m_name;
    }
    }
    ************************************************************************
    Let me know if it is helpful to you
    bye

    take

    wrote:
    > Hi,
    >
    > I have a done some somewhat average tree code and while functional it
    > is not particularly satisfying. Are there any free Swing components
    > which includes an advanced JTree.
    >
    > When I say advanced I mean that out the box it provides
    >
    > a) seperation of events (on select, right click etc.) from the tree
    > b) icon management
    > c) dynamic expansion
    > d) drag and drop
    > etc.
    >
    > Essentially I want to interact something like this...
    >
    > YourTree tree = new YourTree()
    > tree.addOnClick(addMyListener()...);
    > tree.setLeafIcon(myIcon);
    > tree.addOnChildrenRequest(addMyListener()...)
    > tree.addOnSelection(addMyListener()...)
    > tree.addOnDragandDrop(addMyListener()...)
    >
    > etc.
    >
    > Anyone seen such a marvelous thing?
    >
    > thanks
    >
    > Tim
     
    Aman, Mar 24, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,204
  2. sharan
    Replies:
    4
    Views:
    705
    CBFalconer
    Oct 30, 2007
  3. sharan
    Replies:
    2
    Views:
    859
    SM Ryan
    Oct 31, 2007
  4. sharan
    Replies:
    1
    Views:
    708
    CBFalconer
    Oct 30, 2007
  5. george
    Replies:
    0
    Views:
    1,180
    george
    Aug 29, 2008
Loading...

Share This Page