Change Dropdown Width

Hello,

I have a dropdown component whose width is restricted in size. However the options for this dropdown are strings that are longer than the width of the drop. Is there a way to make the dropdown list wider? I just need to see the options when selecting one. i know it wont look right once i select it but that is ok.

49A3655B

I also have this issue. Would be excited to finally have a solution.

Didnā€™t get this working (the 8.0 look and feel complicates things, 7.9 might be easier), but hereā€™s a starting point:

from javax.swing.plaf.basic import BasicComboBoxUI, BasicComboPopup

target = system.gui.getWindow("Main Window").rootContainer.getComponent("Dropdown")

targetUI = target.getUI().getClass()

class CustomComboPopup(BasicComboPopup):
  def __init__(self, target):
    self.target = target
    BasicComboPopup.__init__(self, target)

  def computePopupBounds(self, px, py, pw, ph):
    return BasicComboPopup.computePopupBounds(self, px, py, max(self.target.getPreferredSize().width, pw), ph)
    
class StyledComboBoxUI(targetUI):
  def createPopup(self):
    popup = CustomComboPopup(target)
    return popup

target.setUI(StyledComboBoxUI())

Thanks for the quick reply. Iā€™ll play around with this when i get a few free moments. I am using 7.9. I guessing that I should put this script on something link the mouse pressed event script?

You only want this to run once, so something like windowOpened at the window level would be more appropriate.

Thanks for this snippet! Seems to do exactly what I need. However, when attempting to use it in my scenario (with it on a mouse pressed event for testing) I get

AttributeError: type object 'javax.swing.plaf.basic.BasicComboPopup' has no attribute 'computePopupBounds'

I assume this is a configuration issue where my script canā€™t access the protected method. Are there any workarounds for this?

I ran into the same issue that you did, but was able to resolve it by tweaking the code slightly. I was unable to achieve a ā€œsmartā€ sizing of the popup (i.e. - based on the length of the longest item in the list), but for my purposes, all I really wanted was a way to ā€œforceā€ the popup list to be a certain width, so maybe this very rough snippet will help you.

I put this code in a library called ā€œUtilsā€:

from javax.swing.plaf.basic import BasicComboBoxUI, BasicComboPopup
def setCustomWidthComboPopup(comp, width):
	targetUI = comp.getUI().getClass()
	
	class CustomComboPopup(BasicComboPopup):
		def __init__(self, target):
			self.target = target
			BasicComboPopup.__init__(self, target)
			
		def computePopupBounds(self, px, py, pw, ph):
			return self.super__computePopupBounds(px, py, width, ph)
	
	class StyledComboBoxUI(targetUI):
		def createPopup(self):
			popup = CustomComboPopup(comp)
			return popup
			
	comp.setUI(StyledComboBoxUI())

Then, in the visionWindowOpened event handler for my window, I added these lines:

win = system.gui.getParentWindow(event)

#Get the dropdown component
comp = win.getComponentForPath('Root Container.MetaContainer.MyDropDown')

#Call the 'customWidth' function once on window open
#This example uses a 'static' width of 400, but it could also be 
#some value calculated at runtime, however you'd have to call this function
#again if you want to change it
Utils.setCustomWidthComboPopup(comp, 400)

Some limitations of this quick and dirty implementation:

  • As I mentioned, it doesnā€™t do any fancy ā€˜auto-sizingā€™ of the dropdown popup
  • The dropdown popup is still left-aligned with the dropdown itself (i.e. - it grows to the right). Iā€™m sure if someone wanted it to be right-aligned with the dropdown box, that could be achieved, but I didnā€™t put in the effort

Iā€™m sure someone with more time/skills could improve upon this.

Edit: this worked on v8.0.15

1 Like