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:
2 Likes