Table Dropdown that I can type into

Hey there everyone,
I’m trying to create a table cell where I can select from existing similar values in a dropdown, or type new values into it like a regular table cell or text box… the following code on my PowerTable gets me very close…

def configureEditor(self, colIndex, colName):
    
    if colName == 'SubProject':
    
        ProjectID = self.parent.ProjectID
        
        from javax.swing import AbstractCellEditor, JComboBox
        from javax.swing.table import TableCellEditor
        
        class MyCellEditor(TableCellEditor, AbstractCellEditor):
            def __init__(self, tableComponent):
                self.table = tableComponent
                self.cbValues = None
                
            def getTableCellEditorComponent(self, table, value, isSelected, rowIndex, vColIndex):
                # Get a list of other values existing values
                newData = system.db.runPrepQuery("SELECT DISTINCT C.[SubProject] FROM OMS.[Categories] AS C INNER JOIN CRM.[Milestones] AS M ON M.[ID] = C.[MilestoneID] WHERE M.[ProjectID] = ? ORDER BY C.[SubProject]", [ProjectID])
                newList = []
                
                for row in newData:
                    newList.append(row[colName])
                
                # Build the Combo box with the values from above
                self.cbValues = JComboBox(newList)
                
                # Make the combo box editable
                self.cbValues.setEditable(1)
                
                # Ensure that the existing value is selected.
                self.cbValues.setSelectedItem(value)
    
                return self.cbValues
            
            def getCellEditorValue(self):    
                return self.cbValues.getSelectedItem()
        
        return {'editor': MyCellEditor(self)}

The only drawback is that after I type a new value into it and press enter, it doesn’t stop editing and lose focus… I have to know to click on something else on the screen in order for the value I entered to “stick” by calling the powertable’s onCellEdited event.

Does anyone know how to make the typed editing behave more like a regular text box, where pressing enter commits the edit? Thanks in advance!

Frank. :slight_smile:

I did this with a standard drop down list and a data entry box with an indirect binding to the index of dropdown

Yeah, I would do that too if it were a single dropdown box…except I need this in a table. :frowning:

Ah, sorry, I misunderstood the need. Just one column of the table?

Almost always, if a dataset/table needs to be interacted with I just end up making a template that is a single row and then use a template repeater to fill in the row’s parameter. You could easily use a dropdown component in the “row template.”

You are very close. You need to make your class be an action listener, so that you can react to the combo box's action. Specifically, when you press the enter key, then the action command will be comboBoxEdited. So if you look for that, then you can stop the editing when the enter key is pressed.

def configureEditor(self, colIndex, colName):
    
    if colName == 'SubProject':
    
        ProjectID = self.parent.ProjectID
        
        from javax.swing import AbstractCellEditor, JComboBox
        from javax.swing.table import TableCellEditor
        from java.awt.event import ActionListener

        class MyCellEditor(TableCellEditor, AbstractCellEditor, ActionListener):
            def __init__(self, tableComponent):
                self.table = tableComponent
                self.cbValues = None
                
            def getTableCellEditorComponent(self, table, value, isSelected, rowIndex, vColIndex):
                # Get a list of other values existing values
                newData = system.db.runPrepQuery("SELECT DISTINCT C.[SubProject] FROM OMS.[Categories] AS C INNER JOIN CRM.[Milestones] AS M ON M.[ID] = C.[MilestoneID] WHERE M.[ProjectID] = ? ORDER BY C.[SubProject]", [ProjectID])
                newList = []
                
                for row in newData:
                    newList.append(row[colName])
                
                # Build the Combo box with the values from above
                self.cbValues = JComboBox(newList)
                
                # Register this class as an action listener on the combo box
                self.cbValues.addActionListener(self)

                # Make the combo box editable
                self.cbValues.setEditable(1)
                
                # Ensure that the existing value is selected.
                self.cbValues.setSelectedItem(value)
    
                return self.cbValues
            
            def getCellEditorValue(self):    
                return self.cbValues.getSelectedItem()

            def actionPerformed(self,actionEvent):
                if actionEvent.getActionCommand() == 'comboBoxEdited':
                    self.stopCellEditing()
      
        return {'editor': MyCellEditor(self)}
5 Likes

I suspected it was something like this, but couldn’t suss out the exact syntax to use. Thanks so much! :brown_heart: