Manually set column widths for Vision Power Table in version 8.1

I have a power table on a vision view. I have six columns and I’d like to be able to set the columns widths manually. I can’t find a way to do it.

From the Designer, enable preview mode, use your mouse to resize the column widths, then save the project when you’re happy. Modifications made to components in preview mode are serialized (saved) with the rest of the window, and will be carried over to the client when launched.

Due to how Vision layout works, you might need to go back to the Designer and make some adjustments.

Or if you want to handle it dynamically through scripting here’s a script that I use in a custom method of my power table as a starting point. It can work pretty well depending on how much data your power table has crammed into it. The function gets called from the propertyChange event whenever the data property is updated.

In my case I am setting all boolean columns to a width of 5 so they won’t take up too much space. Also you can put column names in the filter_columns list for any columns you don’t want the script to resize.

	ds = self.viewDataset
	ds_attributes = self.columnAttributesData
	headers = system.dataset.getColumnHeaders(self.viewDataset)
	filter_columns = []  # A list of columns (by name) to remain untouched by this script 
	
	# No point in continuing if the table is empty
	if len(headers) > 0:
	
		# Get list of boolean column indexes. We will make these a fixed smaller size.
		bool_names = [ds_attributes.getValueAt(row, "Name") for row in range(ds_attributes.rowCount) if ds_attributes.getValueAt(row, "treatAsBoolean")]
		bool_col_indexes = [headers.index(name) for name in bool_names if name in headers]
		
		# Get the indexes of the columns that we don't want to resize
		ignore_cols = [headers.index(name) for name in filter_columns if name in headers]
		
		# Hard coded sizing for 2 of the columns
		self.setColumnWidth(0, 30)
		self.setColumnWidth(2, 55)
		ignore_cols.append(0)
		ignore_cols.append(2)
		
		# Get the Graphics object being used by the table. We can use the font render context to measure the size of the text
		g = self.getTable().getGraphics()
		if g is not None:
			lengths = {}
			frc = g.getFontRenderContext()
			for row in range(ds.rowCount):
				for col in range(ds.columnCount):
					if col in ignore_cols:
						continue
					elif col in bool_col_indexes:
						lengths[col] = 5
					else:
						# Check if the text in this cell is wider than the stored column width thus far
						boundPart = self.font.getStringBounds(str(ds.getValueAt(row, col)), frc)
						if int(boundPart.getWidth()) > lengths.get(col, 0):
							lengths[col] = int(boundPart.getWidth())
						
			# Apply the column sizing
			for col in lengths:
				self.setColumnWidth(col, lengths[col])
  • edited to fix a minor issue in the script
1 Like

Nice! I am ok with the simpler solution but good to know for the future if I need it to be more dynamic!

I have discovered that setColumnWidth doesn’t behave properly when some columns are hidden (using the right click headers, check/uncheck enable box in the client).
ie: if i have 10 columns total, and i want to manually set the 4th column very wide, using self.setColumnWidth(4, 500) actually sets the 4th visible column, not the actual 4th column in the dataset.
Which is a total disaster if you are trying to get the column index from getColumnIndex(colName)

Have you encountered this?

I haven’t but I’m not surprised that it doesn’t work well. My script definitely doesn’t handle all possible table states.

I’ve pretty much abandoned that script and have started turning off auto-sizing on table and using the script below that I found somewhere on the forum. I don’t remember who posted it. It causes the table to expand beyond the width of the screen if the data is wide but there are scroll bars and at least all of the data is visible within each cell. I put the script in a function and pass the table component into the script.

	from com.jidesoft.grid import TableUtils
	from javax.swing import JTable
	table = table.getTable()
	table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF)
	TableUtils.autoResizeAllColumns(table)

Ah i think i found the problem. I was using the .data property to determine the column index. But if columns are hidden, you will need to use the .viewDataset property to find column index.
I am also using that script you showed as well, to get the horizontal scrollbars.
But the auto resize doesn’t work too well, and i needed to manually override some of the widths after doing so.

But all is well now, after discovering the .viewDataset property!
Thanks.