Forum Discussion

hogueybear's avatar
hogueybear
Occasional Contributor
10 years ago

Getting the coordinates of Javax.Swing.Tree items

We have swing tree objects where the user drags an item from one tree to another tree.  When this action gets recorded in Jscript it ends up with x, y coordinates like this:   object. drag(132, 245...
  • HKosova's avatar
    10 years ago

    To get coordinates of the selected item:

     

    // JScript
    var path = tree.getSelectionPath(); var bounds = tree.getPathBounds(path); var x = bounds.getCenterX(); var y = bounds.getCenterY();

    This gives you the coordinates of the item's center, relative to the tree object. To convert them to absolute screen coordinates, you can use:

     

    var point = tree.WindowToScreen(x, y);

     

    The solution for an arbitrary item (not the selected one) is similar, but you'll need to get the path object using one of the following:

    tree.getPathForRow(index), where index is the item's row index in the tree (as if the tree was a plain list).

    tree.getModel().getRoot().getChildAt(index1).getChildAt(index2).getPath()