Set Selected Cells in Power Table

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?

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.

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.

1 Like

I wouldn’t recommend getting this hacky but…

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

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)
1 Like

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.

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?

It just gets hackier from here.

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)
1 Like

Consider using JViewport.scrollRectToVisible() instead of Robot.

3 Likes

hi, and now 8.0ignition also has this issue, when you can repair this issue?