Forum Discussion

pjaju's avatar
pjaju
Occasional Contributor
14 years ago

Jtree loading

I am working with JTree object and using the ClickItem method to click on a particular tree node. My question is that, if the node is not found, TC throws error. However, when i open my application, it takes a bit of time to load the tree nodes. While the tree is loading, if i use ClickItem, TC throws an error saying that the object was not found. Once the tree is loaded fully, the command works fine.



I want to know how can i tackle this situation. Can i use a loop:

Do

      Call Aliases.Java_Explorer.Explorer_Main.SwingObject("ExplorerFrame$4", "", 0).SwingObject("null.layeredPane").SwingObject("null.contentPane").SwingObject("YPanel", "", 0).SwingObject("YPanel", "", 0).SwingObject("YSplitPane", "", 0).SwingObject("YSplitPane$InternalSplitPane", "", 0).SwingObject("YSplitPane", "", 0).SwingObject("YSplitPane$InternalSplitPane", "", 0).SwingObject("ExplorerFrame$2", "", 0).SwingObject("YScrollPane", "", 0).SwingObject("JViewport", "", 0).SwingObject("ApplicationScreenSelector", "", 0).SwingObject("Tree").ClickItem("App|Viewers|Stores|Report1")

Until <no error is thrown>

1 Reply


  • Hi Pankaj,





    You can do this in this way:



      Do Until treeNodeExists(tree, itemPath)

        Delay 500

      Loop






    Here is the code of the treeNodeExists script routine along with a sample of its usage:



    Sub Test

      Set tree = Sys.Process("javaw").SwingObject("JFrame", "SwingSet2", 0).SwingObject("JRootPane", "", 0).SwingObject("null.layeredPane").SwingObject("null.contentPane").SwingObject("SwingSet2", "", 0).SwingObject("JTabbedPane", "Tree Demo", 0).SwingObject("JPanel", "", 0).SwingObject("JPanel", "", 0).SwingObject("JScrollPane", "", 0).SwingObject("JViewport", "", 0).SwingObject("TreeDemo$1", "", 0)

      

      itemPath = "Music|Classical|Mozart"

      

      If treeNodeExists(tree, itemPath) Then

        Log.Message("Node exists")

      Else

        Log.Error("Node does not exist")

      End If

    End Sub





    Function treeNodeExists(tree, itemPath)

      itemNames = split(itemPath, "|") 

      currentItem = itemNames(0)

      If IsSupported(tree, "wItems") Then

        Set items = tree.wItems

      Else

        Set items = tree.Items

      End If

      

      For i = 0 To items.Count - 1

        If items.Item(i).Text = currentItem Then

          If UBound(itemNames) = 0 Then

            treeNodeExists = True

          Else

            treeNodeExists = treeNodeExists(items.Item(i), Join(removeFirstItemFromArray(itemNames), "|"))

          End If

          Exit Function

        End If

      Next

      

      treeNodeExists = False

    End Function





    Function removeFirstItemFromArray(arr)

      ReDim result(UBound(arr) - 1)

      For i = 1 To UBound(arr)

        result(i-1) = arr(i)

      Next

      removeFirstItemFromArray = result

    End Function