Forum Discussion

hogueybear's avatar
hogueybear
Occasional Contributor
10 years ago
Solved

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, 55, 78)

 

These statements are sensitive to screen resolution and the size of the application window.

 

The issue is that I can locate the items in the tree using either the name of the item, e.g.,:

 

Dest.ClickItem("null|end_of_the_road|Segment_1|Segment Field_8");

 

Or using the wItems e.g.,:

 

Src.wItems.Item(0).Items.Item(0).Items.Item(12).Click(0);

 

But what I eally want to know is where the item is located.

 

Typically with objects I can use their top, height, and width properties and then replace the drags with a function that will calculate the drag coordinates at run time thus removing the issues of size and resolution.

 

Does anyone know how to figure out the location on the screen of the node of a swing tree?

  • 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()

2 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    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()

    • hogueybear's avatar
      hogueybear
      Occasional Contributor

      Sweet, this is just what I needed.

       

      Thank you.