Power Table column width binding

Dear all:
I have two power tables shown on screen, one above and one below. The data shown in both is similar (as in they share the same column names).
What I would like to be able to do is to resize the columns of one table and the other one should also resize the same column so to match both widths in both tables.
Hope I made myself clear enough.
Thanks in advance for your suggestions.

You can put this script in the Initialize extension function of one of your power tables.

	table2 = self.parent.getComponent("Power Table 2")
	self.table.setColumnModel(table2.table.getColumnModel())

However this does make column re-sizing awkward on the second table. You will have to try it to see if that is a problem or not.

A workaround would be to hide the header on the second table since they have the same columns. That way users would have to use the 1st table to resize columns

1 Like

Thanks JGJohnson.
I’m not sure what the underlying Java objects do in this case, but apparently doing this ties both tables together so I only have to resize one to resize the other. Unfortunately once they are tied the second table won’t responde to resizing on its own columns and, as you mentioned, I should only resize the columns of the first table afterwards.
Furthermore, now that both tables are tied I don’t know how to untie them!
Kind of works but not exactly as I wanted it to. It would be nice to resize any of the tables and “copy” the columns sizes to the other table. I’ll look more into it and report any findings.
Thanks.

Well, you've discarded the column model for the 2nd table when you replaced it with the first table's model. You'll need to experiment with table dataset refreshes and/or column properties dataset refreshes to see if you can regenerate it.

Consider manipulating the column properties dataset at runtime -- possibly by unidirectional bindings from the other table via intermediate custom properties.

Would you hint me in the proper direction as to how to achieve this? I could not find the column width property via scripting. Should I be accesing a property in the underlying Java table? There seem to be a lot of hidden properties for the Power Tables!

Every table has a column attributes dataset that is expected to have a row for each column displayed. This dataset is created and updated by the Table Customizer. The table customizer doesn’t show a width setting for columns, but it ends up in this dataset, at the far right. Manually adjust those values by editing the column attributes dataset directly, instead of in the customizer. Adjusting column widths in preview mode updates this dataset, too. You can also programmatically create/edit this dataset to change the appearance of a table on the fly.

Are you referring to the columnAttributesData? That dataset has the following columns:

#Script running from a button on the same screen as the PowerTable
cad = event.source.parent.getComponent('PowerTable').columnAttributesData
print cad.getColumnNames()

>> [name, dateFormat, editable, filterable, hidden, horizontalAlignment, label, locale, numberFormat, prefix, sortable, suffix, treatAsBoolean, verticalAlignment, wrapText]

Could not find this column you mention.

Hmm. It's in mine. It might only show up if you drag column widths in the designer. But you can add it yourself.

If I add a column to the “Column Attributes Data” property I get a

java.lang.IllegalArgumentException: No such property: width

Ok, I think I found a way to do what I wanted. It is not exactly the result I hoped to achieve but it’s close enough.

What I do is create a context menu on each of the tables that calls a function defined in the container of both tables (but could be anywhere basically).

def onPopupTrigger(self, rowIndex, colIndex, colName, value, event):
	"""
	(...)
	"""
	
	def updateColumns(event):
		self.parent.setColumnWidths(self)
		
	menuItemNames     = ['Copy widths to other tables']			#Create menu with elements
	menuItemFunctions = [updateColumns]					#Assign functions
	menu = system.gui.createPopupMenu(menuItemNames, menuItemFunctions)	#Create menu with above items
	menu.show(event)													#Show menu on right click

Then, the function that does the magic defined as a Custom Method in the container of the other tables:

def setColumnWidths(self, table):
	"""
	(...)
	"""
	
	jtable   = table.getTable()		#javax.swing.JTable
	data     = table.viewDataset		#viewDataset to replicate
	colNames = list(data.getColumnNames())	#List with column names
	
	#Tables to receive the column widths
	tables = [
		self.getComponent('ptTop'),
		self.getComponent('ptMiddle'),
		self.getComponent('ptBottom')
		]
	
	#Create dataset with column widths from received table
	headers = ['Column','Width']
	values = []
	for col in colNames:
		values.append([colNames.index(col), jtable.getColumn(col).width]) 
	colDataset = system.dataset.toDataSet(headers, values)
	
	#Update the width of the columns in the other tables
	for tbl in tables:
		if tbl != table:	#Only update the other tables
			for row in xrange(colDataset.rowCount):
				tabla.setColumnWidth( colDataset.getValueAt(row, 'Column'), colDataset.getValueAt(row, 'Width') )

Not sure if it’s the optimum solutions but it works for now.
Hope it helps someone else!

Shouldn’t the last line of setColumnWidths reference tbl.setColumnWidth? tabla is not referenced anywhere else.

You are right. Sorry, there was a mistranslation from my code.
The last for I have it programmed as:

#Actualizo el ancho de las columnas de las otras tablas
	for tabla in tablas:
		if table != tabla:	#Solo actualizo el resto de las tablas
			for row in xrange(colDataset.rowCount):
				tabla.setColumnWidth( colDataset.getValueAt(row, 'Columna'), colDataset.getValueAt(row, 'Ancho') )ho') )