# Manually set column widths for Vision Power Table in version 8.1

**URL:** <https://forum.inductiveautomation.com/t/manually-set-column-widths-for-vision-power-table-in-version-8-1/49547>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [August 4, 2021, 1:37pm UTC](https://forum.inductiveautomation.com/t/manually-set-column-widths-for-vision-power-table-in-version-8-1/49547 "2021-08-04T13:37:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael.black](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@michael.black](https://forum.inductiveautomation.com/u/michael.black)\
**Post date:** [August 4, 2021, 1:37pm UTC](https://forum.inductiveautomation.com/t/manually-set-column-widths-for-vision-power-table-in-version-8-1/49547/1 "2021-08-04T13:37:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paul.Scott](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul.scott/32/4578_2.png) [@Paul.Scott](https://forum.inductiveautomation.com/u/Paul.Scott)\
**Post date:** [August 4, 2021, 3:42pm UTC](https://forum.inductiveautomation.com/t/manually-set-column-widths-for-vision-power-table-in-version-8-1/49547/2 "2021-08-04T15:42:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [August 4, 2021, 6:56pm UTC](https://forum.inductiveautomation.com/t/manually-set-column-widths-for-vision-power-table-in-version-8-1/49547/3 "2021-08-04T18:56:41Z")

</div>

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.

```auto
	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

---

<div class="post-metadata">

**Author:** ![michael.black](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@michael.black](https://forum.inductiveautomation.com/u/michael.black)\
**Post date:** [August 9, 2021, 2:44pm UTC](https://forum.inductiveautomation.com/t/manually-set-column-widths-for-vision-power-table-in-version-8-1/49547/4 "2021-08-09T14:44:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [August 4, 2022, 9:37pm UTC](https://forum.inductiveautomation.com/t/manually-set-column-widths-for-vision-power-table-in-version-8-1/49547/5 "2022-08-04T21:37:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [August 4, 2022, 10:06pm UTC](https://forum.inductiveautomation.com/t/manually-set-column-widths-for-vision-power-table-in-version-8-1/49547/6 "2022-08-04T22:06:30Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![richardNZ15](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/richardnz15/32/33373_2.png) [@richardNZ15](https://forum.inductiveautomation.com/u/richardNZ15)\
**Post date:** [August 4, 2022, 10:30pm UTC](https://forum.inductiveautomation.com/t/manually-set-column-widths-for-vision-power-table-in-version-8-1/49547/7 "2022-08-04T22:30:14Z")

</div>

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.
