How To Change Vision Dropdown Component Scrollbar Width dynamically?

Hi

I'm trying to figure out how to dynamically change the width of the scrollbar on a dropdown component in vision.

I've learned how to do this for a power table component using the the following scripting:

from java.awt import Dimension
from java.lang import Integer

new_scrollbar_width = Dimension(40, Integer.MAX_VALUE)
comp = system.gui.getParentWindow(event).getRootContainer().getComponent('ComponentName')
comp.getVerticalScrollBar().setPreferredSize(new_scrollbar_width)

I tried to apply the same script to the dropdown but the dropdown component does not appear to have a getVerticalScrollBar function. Is there a similar function for dropdowns? Is what I'm even trying to accompolish possible?

I know I can set the scrollbar width in the project properties window, but I only want to for one specific page when in touchscreen mode.

This could be simplified a bit in this way, so that the Dimension that's already there can be used and adjusted without the need for awkward imports:

newWidth = 100
scrollbar = event.source.parent.getComponent('Power Table').verticalScrollBar
preferredSize = scrollbar.getPreferredSize()
preferredSize.width = newWidth
scrollbar.setPreferredSize(preferredSize)

The reason why that method exists directly on a power table is because the VisionAdvancedTable class extends JScrollPane [via AbstractVisionScrollPane], so it inherits the verticalScrollBar property from the JScrollPane class.

However, the scrollpane in a dropdown component is harder to get to because it's embedded in a popup menu that's not directly nested in the core component itself, but to answer your question, it can be retrieved in this way:

# Get the dropdown [popup] from the UI
dropdownComponent = event.source.parent.getComponent('Dropdown')
dropdown = dropdownComponent.UI.getAccessibleChild(dropdownComponent, 0)

# The 'JScrollPane' will almost certainly be .getComponent(0),
# ...but for lack of trust when it comes to component heiarchies, validation is performed
for component in dropdown.components:
	if component.__class__.__name__ == 'JScrollPane':
		
		# Get the scroll bar from the scroll pane, and do something with it
		scrollbar = component.verticalScrollBar

Unfortunately, I don't have enough time this morning to develop a way to adjust its size in a single component, but perhaps knowing how to get to it will be enough to get you unstuck, or perhaps somebody else will chime in on where to go from here.

Edit: I poked around at it a bit and found that simply replacing the original scrollpane with custom JScrollPane gets the job done.

Example:

from javax.swing import JScrollPane
from java.awt import Dimension
newWidth = 50
dropdownComponent = event.source.parent.getComponent('Dropdown')
dropdown = dropdownComponent.UI.getAccessibleChild(dropdownComponent, 0)
scrollPane = dropdown.getComponent(0)
view = scrollPane.viewport.view
newScrollPane = JScrollPane(view)
scrollbar = newScrollPane.verticalScrollBar
scrollbar.setPreferredSize(Dimension(newWidth, scrollbar.height))
dropdown.remove(scrollPane)
dropdown.add(newScrollPane, 0)

Result:

3 Likes

Justin, thank you for the help.

The rewrite for the power table is so much cleaner and I will be using that instead moving forward.

What you sent for the scroll appears to be working the way I hoped. The only issue I'm now facing is that the width of the scroll pane appears to be reverted back to the default width of the component with this change. See below:

Do you know what the code would be to adjust that sizing? For example, in the screenshot, the width of the dropdown should be 300.

I've seen this in the forum before:
Column Widths of Dropdown List Table Mode

...but perhaps I did something wrong and didn't realize it when I experimented with this a couple of mornings ago because, at the time, I couldn't get the original scrollbar to accept my size adjustments. This is much simpler, and it gives the desired result:

from java.awt import Dimension
newWidth = 50
dropdownComponent = event.source.parent.getComponent('Dropdown')
dropdown = dropdownComponent.UI.getAccessibleChild(dropdownComponent, 0)
scrollPane = dropdown.getComponent(0)
scrollbar = scrollPane.verticalScrollBar
scrollbar.setPreferredSize(Dimension(newWidth, scrollbar.height))

Result:

Yep, that did it!

Thank you for the help, Justin.

1 Like