Power Table - Dropdown Configuration

Update: I figured out how to dynamically set the dropdown menu widths to the width of the longest text item in the list.

Here is the result:
image

The main problem was that the scroll pane had a maximum width setting that was equal to the cell size. After fixing this, code had to be added to ensure that the JComboBox was showing and the dropdown menu was open prior to the changes being applied.

Here is the working script:

Second Attempt Script
#def onMouseClick(self, [...], event):
	from javax.swing import JComboBox, JScrollPane
	from java.awt import Dimension
	from java.awt.font import FontRenderContext, TextLayout
	from java.awt import Font
	def setSizes(dropdown):
		dropdown.setMaximumSize(Dimension(2*maxItemWidth, 2*dropdown.getPreferredSize().height))
		dropdown.setMinimumSize(Dimension(0, 0))
		dropdown.setPreferredSize(Dimension(maxItemWidth, dropdown.getPreferredSize().height))
		for component in dropdown.getComponents():
			if isinstance(component, JScrollPane):
				setSizes(component)
				break
	if hasattr(self.table.getCellEditor(rowIndex, colIndex), 'component'):
		editorComponent = self.table.getCellEditor(rowIndex, colIndex).component
		if isinstance(editorComponent, JComboBox):
			tableFont = self.font
			widthSettingFont = Font(Font.MONOSPACED, tableFont.style, tableFont.size)
			dropdown = editorComponent.getUI().getAccessibleChild(editorComponent, 0)
			items = [
				editorComponent.renderer.getListCellRendererComponent(dropdown.getList(), editorComponent.model.getElementAt(row), -1, False, False).getText()
				for row in xrange(editorComponent.model.size)
			]
			maxItemWidth = int(TextLayout(max(items, key=len), widthSettingFont, FontRenderContext(None, False, False)).bounds.width)
			if not editorComponent.popupVisible and editorComponent.showing:
				editorComponent.setPopupVisible(True)
			setSizes(dropdown)

Edit: This script has a bug where if you click on the same cell multiple times the dropdown regresses due to the way the cell retains focus and prevents the mouseClick extension function from firing every time. See post 25 for the updated solution.

5 Likes