Eslam Hamdy
Eslam Hamdy

Reputation: 7396

Jtree get object based on mouse click?

I use this code fragment to perform some action on a tree when the mouse is double-clicked: open a window and get the node which was double clicked by the mouse, but it doesn't return anything, it returns null:

MouseListener ml = new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
        int selRow = contactTree.getRowForLocation(e.getX(), e.getY());
        TreePath selPath = contactTree.getPathForLocation(e.getX(), e.getY());
        System.out.println(contactTree.getEditingPath());
        Account memberToChat;
        if(selRow != -1) {
            if(e.getClickCount() == 1) {

            }
            else if(e.getClickCount() == 2) {
                new ChatWindow().setVisible(true);
                memberToChat=(Account)node.getUserObject(); // node is declared somewhere in the class as   DefaultMutableTreeNode node
                System.out.println(memberToChat.getFirstName()+" "+memberToChat.getEmail());
            }
        }
    }
};

Upvotes: 2

Views: 2285

Answers (1)

Related Questions