How to get Table Column Width?

Dear Inductive Team,

We had a requirement where we need to get Table Column width in Event Handler. We mean, Is there any function similar to setColumnWidth() function where we can get Column width on Runtime (In Launched Project) ?

We had a look at columnAttributesData for table component. However, values in this dataset are not changing runtime.

You can use this:

table = event.source.parent.getComponent("Table")

print table.table.columnModel.getColumn(2).getWidth()

Where ‘2’ is the column index, meaning the third column in your table. Hope this helps, take care.

Thank you Michael. It works perfect.

One more question. Is there any event that will fire when we change Column width in Launched Project ? We tried with PropertyChange, MousePressed Events. However these events are not firing on Column (in Column Header) width change.

There is a way to do it, but it is a little tricky. There is nothing listening to the column width event by default, but you can implement your own propertyChangeListener. You’d want to put something like this on the visionWindowOpened event handler of a window:

table = event.source.rootContainer.getComponent("Table")
from java.beans import PropertyChangeListener
class MyListener(PropertyChangeListener):
	def propertyChange(self, event):
		if event.propertyName == "width":
			print event.newValue
			
table.table.columnModel.getColumn(0).addPropertyChangeListener(MyListener())

The ‘print event.newValue’ is where the new value of the column width gets printed, and you’ll notice you have to implement the listener on every column you’re interested in. In this case, I’ve hard-coded column 0 (the last line). Hope this is helpful, talk to you later.

Michael,

It worked perfect. Here we go with one more issue. We set autoResizeMode to off for Table component (Ex: tblData) for better column data visibility which shows Horizontal scroll bar. Why we need to do all this (fetching column width on runtime, event for triggering the change etc.) ? We are trying to put another Table component (Ex: tblSearch) exactly below the tblData for searching. We need to have same no. of columns in tblSearch as in tblData and same columns position (with same width as well) so that they can search it easily.

How we can get Scroll Position ? Is there better way for achieving above requirement ?

You can get an instance of the horizontal scroll bar of a component by calling getHorizontalScrollBar(), then you can use getValue() and setValue() to match up the scrollbars of the two tables:

comp = event.source.parent.getComponent("Table") comp2 = event.source.parent.getComponent("Table 1") value = comp.getHorizontalScrollBar().getValue() comp2.getHorizontalScrollBar().setValue(value)

The value that is returned from getValue is an integer, which is the number of pixels that the scrollbar has been offset from the left arrow.

James and Michael,

Your code works perfect. Thank you for your quick responses.

We are glad to work on Ignition with your support.