# Set Selected Cells in Power Table

**URL:** <https://forum.inductiveautomation.com/t/set-selected-cells-in-power-table/15955>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [November 10, 2017, 1:49am UTC](https://forum.inductiveautomation.com/t/set-selected-cells-in-power-table/15955 "2017-11-10T01:49:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaynes](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmaynes/32/6852_2.png) [@cmaynes](https://forum.inductiveautomation.com/u/cmaynes)\
**Post date:** [November 10, 2017, 1:49am UTC](https://forum.inductiveautomation.com/t/set-selected-cells-in-power-table/15955/1 "2017-11-10T01:49:20Z")

</div>

Hello, I have a power table which displays all of the filters for my reports. I can get all of the selected cells now and store them away, but if the user wants to return to editing their filters, I want to have all of the filters they previously selected in the table selected automatically when they go to edit them.

To clarify, I do not want to just select an entire row or column, I want to select individual cells in each column of the table. I have a dictionary with the column name as the key and then a list of the filter values. I want to then loop through those columns and select the cells with the values in my dictionary.

Is there a way to select non-contiguous cells in a power table?

---

<div class="post-metadata">

**Author:** ![samngeru](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/samngeru/32/6007_2.png) [@samngeru](https://forum.inductiveautomation.com/u/samngeru)\
**Post date:** [November 11, 2017, 4:32am UTC](https://forum.inductiveautomation.com/t/set-selected-cells-in-power-table/15955/2 "2017-11-11T04:32:59Z")

</div>

You may be able to build of off this example. Make sure the Selection Mode property for the Power Table is set to Multiple Interval. This script is on a Button in the same window as the Power Table.

```auto
table = event.source.parent.getComponent("Power Table").getTable()

model = table.getSelectionModel()
model.clearSelection()
model.addSelectionInterval(1, 1)
model.addSelectionInterval(3, 3)
model.addSelectionInterval(5, 5)

```

---

<div class="post-metadata">

**Author:** ![cmaynes](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmaynes/32/6852_2.png) [@cmaynes](https://forum.inductiveautomation.com/u/cmaynes)\
**Post date:** [November 12, 2017, 8:30pm UTC](https://forum.inductiveautomation.com/t/set-selected-cells-in-power-table/15955/3 "2017-11-12T20:30:12Z")

</div>

> [@samngeru](#):
>
> table = event.source.parent.getComponent("Power Table").getTable()
> 
> model = table.getSelectionModel()  
> model.clearSelection()  
> model.addSelectionInterval(1, 1)  
> model.addSelectionInterval(3, 3)  
> model.addSelectionInterval(5, 5)

This does not work if I have **Non-contiguous selection** set to **True.** This selection allows the user to select individual cells rather than entire rows and I need this functionality for my program.

---

<div class="post-metadata">

**Author:** ![samngeru](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/samngeru/32/6007_2.png) [@samngeru](https://forum.inductiveautomation.com/u/samngeru)\
**Post date:** [November 13, 2017, 7:32am UTC](https://forum.inductiveautomation.com/t/set-selected-cells-in-power-table/15955/4 "2017-11-13T07:32:19Z")

</div>

I wouldn’t recommend getting this hacky but…

Obviously the cells you want to be selected have to be visible.

```auto
from java.awt import Robot
from java.awt.event import InputEvent, KeyEvent

component = event.source.parent.getComponent('Power Table')
component.nonContiguousCellSelection = 0
component.nonContiguousCellSelection = 1

scrollBar = component.getVerticalScrollBar()
scrollBar.setValue(scrollBar.getMinimum())

scrollBar = component.getHorizontalScrollBar()
scrollBar.setValue(scrollBar.getMinimum())

table = component.getTable()

p = table.getLocationOnScreen()
r = Robot()

r.keyPress(KeyEvent.VK_CONTROL)

cellsToSelect = \
[
	[1,1],
	[3,1],
	[5,1],
	[7,1]
]

for cell in cellsToSelect:
	cellRect = table.getCellRect(cell[0], cell[1], True)
	r.mouseMove(p.x + cellRect.x + cellRect.width / 2, p.y + cellRect.y + cellRect.height / 2);
	r.mousePress(InputEvent.BUTTON1_MASK)
	r.mouseRelease(InputEvent.BUTTON1_MASK)

r.keyRelease(KeyEvent.VK_CONTROL)

```

---

<div class="post-metadata">

**Author:** ![cmaynes](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmaynes/32/6852_2.png) [@cmaynes](https://forum.inductiveautomation.com/u/cmaynes)\
**Post date:** [November 13, 2017, 2:12pm UTC](https://forum.inductiveautomation.com/t/set-selected-cells-in-power-table/15955/5 "2017-11-13T14:12:09Z")

</div>

Clever, clever Mr.Robot - **samngeru**  
Thank you for your solution. It gets the job done.

If I reposition the mouse after the fact, the user will not notice what happened.

---

<div class="post-metadata">

**Author:** ![cmaynes](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmaynes/32/6852_2.png) [@cmaynes](https://forum.inductiveautomation.com/u/cmaynes)\
**Post date:** [March 14, 2018, 11:53pm UTC](https://forum.inductiveautomation.com/t/set-selected-cells-in-power-table/15955/6 "2018-03-14T23:53:27Z")

</div>

I just found a bug with this solution…

If the cell is not visible, then we need to scroll the cell into view. I have tried doing this, but robot uses coordinates relative to the window and not the table component.

How can we scroll to the cell and select it?

---

<div class="post-metadata">

**Author:** ![samngeru](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/samngeru/32/6007_2.png) [@samngeru](https://forum.inductiveautomation.com/u/samngeru)\
**Post date:** [March 21, 2018, 8:39pm UTC](https://forum.inductiveautomation.com/t/set-selected-cells-in-power-table/15955/7 "2018-03-21T20:39:08Z")

</div>

It just gets hackier from here.

```auto
def fun():
	from java.awt import Robot
	from java.awt.event import InputEvent, KeyEvent
	import time
	component = event.source.parent.getComponent('Power Table')
	component.nonContiguousCellSelection = 0
	component.nonContiguousCellSelection = 1
	
	table = component.getTable()
	
	r = Robot()
	
	cellsToSelect = \
	[
		[1,1],
		[3,1],
		[5,1],
		[7,1],
		[8,1],
		[2,4]
	]
	
	for cell in cellsToSelect:
		scrollBar = component.getVerticalScrollBar()
		scrollBar.setValue(scrollBar.getMinimum())
		time.sleep(.1)
		scrollBar = component.getHorizontalScrollBar()
		scrollBar.setValue(scrollBar.getMinimum())
		time.sleep(.1)
		r.keyPress(KeyEvent.VK_CONTROL)
		cellRect = table.getCellRect(cell[0], cell[1], True)
		table.scrollRectToVisible(cellRect)
		p = table.getLocationOnScreen()
		r.mouseMove(p.x + cellRect.x + cellRect.width / 2, p.y + cellRect.y + cellRect.height / 2)
		time.sleep(.5)
		r.mousePress(InputEvent.BUTTON1_MASK)
		r.mouseRelease(InputEvent.BUTTON1_MASK)
	
	r.keyRelease(KeyEvent.VK_CONTROL)
system.util.invokeAsynchronous(fun)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 21, 2018, 8:50pm UTC](https://forum.inductiveautomation.com/t/set-selected-cells-in-power-table/15955/8 "2018-03-21T20:50:33Z")

</div>

Consider using [JViewport.scrollRectToVisible()](https://docs.oracle.com/javase/7/docs/api/javax/swing/JViewport.html#scrollRectToVisible(java.awt.Rectangle)) instead of `Robot`.

---

<div class="post-metadata">

**Author:** ![stefan.wang](https://avatars.discourse-cdn.com/v4/letter/s/b19c9b/32.png) [@stefan.wang](https://forum.inductiveautomation.com/u/stefan.wang)\
**Post date:** [August 30, 2019, 9:21am UTC](https://forum.inductiveautomation.com/t/set-selected-cells-in-power-table/15955/9 "2019-08-30T09:21:06Z")

</div>

hi， and now 8.0ignition also has this issue， when you can repair this issue？
