# Tree item context menu

**URL:** <https://forum.inductiveautomation.com/t/tree-item-context-menu/3857>\
**Category:** Ignition\
**Created:** [January 13, 2012, 7:43pm UTC](https://forum.inductiveautomation.com/t/tree-item-context-menu/3857 "2012-01-13T19:43:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://forum.inductiveautomation.com/u/Robert)\
**Post date:** [January 13, 2012, 7:43pm UTC](https://forum.inductiveautomation.com/t/tree-item-context-menu/3857/1 "2012-01-13T19:43:49Z")

</div>

I want to create a context menu based on where the user right-clicks on the tree view component.  
How can I tell what element (if any) the user is right clicking on?

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [January 13, 2012, 10:25pm UTC](https://forum.inductiveautomation.com/t/tree-item-context-menu/3857/2 "2012-01-13T22:25:06Z")

</div>

The only way is through the selected item or selected path properties of the tree view. If the selected item is -1 nothing is selected. The only problem is when the user right clicks on a different item than the one that is selected. There is no way of detecting that situation right now.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://forum.inductiveautomation.com/u/Robert)\
**Post date:** [January 13, 2012, 11:58pm UTC](https://forum.inductiveautomation.com/t/tree-item-context-menu/3857/3 "2012-01-13T23:58:59Z")

</div>

no hidden java class functions/properties I can reference?

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [January 14, 2012, 12:23am UTC](https://forum.inductiveautomation.com/t/tree-item-context-menu/3857/4 "2012-01-14T00:23:33Z")

</div>

Ok, ok. Here you go:`if event.button == event.BUTTON3:
	tree = event.source.getViewport().getView()
	row = tree.getRowForLocation(event.x, event.y)
	path = tree.getPathForLocation(event.x, event.y)
	pathStr = ""
	if path != None:
		for item in path.getPath():
			pathStr += str(item) + event.source.separationCharacter
		pathStr = pathStr[1:-1]
	event.source.setSelectedPath(pathStr)
	print event.source.selectedPath
	print event.source.selectedItem`

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://forum.inductiveautomation.com/u/Robert)\
**Post date:** [January 16, 2012, 4:57pm UTC](https://forum.inductiveautomation.com/t/tree-item-context-menu/3857/5 "2012-01-16T16:57:55Z")

</div>

![:prayer:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/e5a7e3b63facc151ea73e3b45db7bdb3d4bdc316.gif ":prayer:")

U da man Travis  
 ![:prayer:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/e5a7e3b63facc151ea73e3b45db7bdb3d4bdc316.gif ":prayer:")

---

<div class="post-metadata">

**Author:** ![chris3145](https://avatars.discourse-cdn.com/v4/letter/c/96bed5/32.png) [@chris3145](https://forum.inductiveautomation.com/u/chris3145)\
**Post date:** [April 11, 2018, 6:42pm UTC](https://forum.inductiveautomation.com/t/tree-item-context-menu/3857/6 "2018-04-11T18:42:40Z")

</div>

I used Travis’s code, but I found it messes up if the view has been scrolled. Here’s a tweak that uses SwingUtilities to convert the click coordinates to compensate for scrolling in the Tree View.

```auto
if event.button == event.BUTTON3:	
	from javax.swing import SwingUtilities
	
	treeViewport = event.source.getViewport()
	treeView = treeViewport.getView()
	viewClick = SwingUtilities.convertPoint(treeViewport, event.x, event.y, treeView)

	row = treeView.getRowForLocation(viewClick.x, viewClick.y)
	path = treeView.getPathForLocation(viewClick.x, viewClick.y)
	pathStr = ""
	if path != None:
		for item in path.getPath():
			pathStr += str(item) + event.source.separationCharacter
		pathStr = pathStr[1:-1]

	event.source.setSelectedPath(pathStr)
	print event.source.selectedPath
	print event.source.selectedItem

```
