Anand Biradar
Anand Biradar

Reputation: 79

Java Jtree:how to display selected node

I have a JTree. When i select a node from Jtree , I want to display selected node.

DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); 
type=selectedNode.toString();

this code is working perfectly for Left click,but when i use it for right mouse click it wont get selected nor displayed.

Upvotes: 0

Views: 72

Answers (1)

DRastislav
DRastislav

Reputation: 1882

can you try this method?

public void mouseClicked(MouseEvent e) {

    if (SwingUtilities.isRightMouseButton(e)) {

        int row = tree.getClosestRowForLocation(e.getX(), e.getY());
        tree.setSelectionRow(row);
        popupMenu.show(e.getComponent(), e.getX(), e.getY());
    }
}

Upvotes: 1

Related Questions