JTree association with data

Discussion in 'Java' started by villumanati@gmail.com, Feb 15, 2007.

  1. Guest

    I am trying to write a program that "associates" a node in JTree with
    data. If you take for example an Employee which would be a node in a
    jtree. What would i need to do to get data(name, ssn,etc..)
    associated with the employee to dispaly once the node was clicked
    on.


    Thanks.

    p.s. I have already figured out how to contruct and add items a jtree.
    , Feb 15, 2007
    #1
    1. Advertising

  2. wrote:
    > I am trying to write a program that "associates" a node in JTree with
    > data. If you take for example an Employee which would be a node in a
    > jtree. What would i need to do to get data(name, ssn,etc..)
    > associated with the employee to dispaly once the node was clicked
    > on.
    >
    >
    > Thanks.
    >
    > p.s. I have already figured out how to contruct and add items a jtree.
    >



    I read this and thought "I've never used a JTree before, I wonder how
    hard that might be"

    ....

    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;

    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.SwingUtilities;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreePath;

    public class TreeTest extends JPanel {

    JTree tree;

    TreeTest() {
    Person ceo = new Person("Steve Gates", "123-456-789");
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(ceo);

    Person fd = new Person("Bill Jobs", "013-453-263");
    DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(fd);
    root.add(child1);

    Person hrd = new Person("Darl Torvalds", "987-765-324");
    DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(hrd);
    root.add(child2);

    final JTree tree = new JTree(root);
    add(new JScrollPane(tree));

    MouseListener ml = new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    int selRow = tree.getRowForLocation(e.getX(), e.getY());
    TreePath selPath = tree.getPathForLocation(
    e.getX(), e.getY());
    DefaultMutableTreeNode node =
    (DefaultMutableTreeNode)
    selPath.getLastPathComponent();
    Person x = (Person)node.getUserObject();
    if (selRow != -1) {
    if (e.getClickCount() == 2) {
    JOptionPane.showMessageDialog(
    null, x.allAboutMe());
    }
    }
    }
    };
    tree.addMouseListener(ml);
    }

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    JFrame f = new JFrame("Tree Test");
    f.add(new TreeTest());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
    }
    });
    }
    }

    class Person {
    String name;

    String ssn;

    Person(String name, String ssn) {
    this.name = name;
    this.ssn = ssn;
    }

    public String allAboutMe() {
    return "Name: " + name + ", SSN: " + ssn;
    }

    public String toString() {
    return name;
    }
    }


    Doubtless the above can be improved enormously. I just quickly cut &
    pasted the examples in the Javadocs and looked for suitable methods to
    get objects out. If I was to use a JTree for real I'd try to use
    generics to get rid of the type casts and maybe look for other types of
    listeners that could be used (node selected?)
    RedGrittyBrick, Feb 15, 2007
    #2
    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. MK
    Replies:
    2
    Views:
    2,471
  2. Tim Hoffman

    JTree leaf association to an object

    Tim Hoffman, Feb 11, 2004, in forum: Java
    Replies:
    3
    Views:
    735
    Tim Hoffman
    Feb 11, 2004
  3. Sorin Gherman

    How to use JTree with very large data?

    Sorin Gherman, Sep 13, 2004, in forum: Java
    Replies:
    6
    Views:
    4,254
    Rogan Dawes
    Sep 15, 2004
  4. jumpz
    Replies:
    3
    Views:
    615
    Jean-Baptiste Nizet
    Nov 12, 2007
  5. rina sharma
    Replies:
    1
    Views:
    816
    Registered User
    Feb 14, 2012
Loading...

Share This Page