This is great! I appreciate all the work you put into this, it works perfectly.
I altered the code a bit to support multiple dropdowns on a page. Now all that's needed to format additional dropdowns, is an addition of a dictionary with the appropriate keys to the "dropdowns" list.
I also added support for configuring the width of multiple columns (In the case where more than 2 columns might be displayed).
# list of dictionaries with path to dropdown and max/preffered width parameters
dropdowns = [
{
"dropdown": system.gui.getParentWindow(event).getComponentForPath('Root Container.Dropdown'), # Path to dropdown component
"max_width" : [70], # list of max collumn widths
"preffered_width": [70] # list of preffered collumn widths
},
{
"dropdown": system.gui.getParentWindow(event).getComponentForPath('Root Container.Dropdown 1'), # Path to dropdown component
"max_width" : [100, 70], # list of max collumn widths
"preffered_width": [100, 70] # list of preffered collumn widths
},
]
###################################################################################################
from com.inductiveautomation.factorypmi.application.components.combobox import TableComboBoxPopup
from javax.swing import JTable, JScrollPane, JViewport
from java.awt.event import MouseAdapter
#Can be launched from a button for testing but belongs in the parent window's internalFrameOpened event handler
def getPopup(dropdown):
ui = dropdown.getUI()
if ui is not None:
for child in range(ui.getAccessibleChildrenCount(dropdown)):
component = ui.getAccessibleChild(dropdown, child)
if isinstance(component, TableComboBoxPopup):
return component
def getScrollPane(dropdown):
popup = getPopup(dropdown)
if popup is not None:
for component in popup.getComponents():
if isinstance(component, JScrollPane):
return component
def getViewport(dropdown):
scrollPane = getScrollPane(dropdown)
if scrollPane is not None:
for component in scrollPane.getComponents():
if isinstance(component, JViewport):
return component
def getTable(dropdown):
viewport = getViewport(dropdown)
if viewport is not None:
for component in viewport.getComponents():
if isinstance(component, JTable):
return component
class CustomMouseListener(MouseAdapter):
def __init__(self, dropdown, maxWidth, preferredWidth):
self.dropdown = dropdown
self.maxWidth = maxWidth
self.preferredWidth = preferredWidth
def mouseClicked(self, event):
setColumnWidths(self.dropdown, self.maxWidth, self.preferredWidth)
def setColumnWidths(dropdown, maxWidth, preferredWidth):
if dropdown.isPopupVisible():
dropdown.setPopupVisible(False)
else:
dropdown.setPopupVisible(True)
for i in range(min(len(maxWidth), len(preferredWidth), dropdown.data.getColumnCount())):
column = getTable(dropdown).getColumnModel().getColumn(i)
if column is not None:
column.setMaxWidth(maxWidth[i])
column.setPreferredWidth(preferredWidth[i])
else:
print 'Column could not be found!'
for item in dropdowns:
scrollPane = getScrollPane(item["dropdown"])
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)
table = getTable(item["dropdown"])
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN)
for mouseListener in item["dropdown"].getMouseListeners():
item["dropdown"].removeMouseListener(mouseListener)
item["dropdown"].addMouseListener(CustomMouseListener(item["dropdown"], item["max_width"], item["preffered_width"]))