Table cell dropdown query

I Need to generate a dropdown from a cellclick event on a power table. the “option” function it is not enough, as i need the cell to be an editable dropdown to the user, with a query, so i can filter the dropdown.data while the user is doing the input on the cell.

once the user finish typing and pressed enter , i have a celledit script. if the user types the whole partnumber in the cell edit then presses enter or tab i’m running a script for the table to fill the rest of the column in that row. but if the user select the value by clicking on the dropdown instead of typing it i need the celledit to run the script.

any ideas?

You can configure the cell renderer to be a jDropDown in the configure cell extension function. It sounds like you know exactly what to do.

1 Like

thanks, I’m actually very new to Java. anyway you can provide a short example to help me get going?

Perfect abstractions has a really great blog on how to do just that, with the exception that you’ll probably want a JComboBox. Here is the blog link.

http://blog.perfectabstractions.com/2017/03/10/getting-even-more-out-of-your-power-tables/

Also a quick google search on any of these java components will expose to you the properties that you might want to edit etc.

Your code might look something like this in the configure cell extension function.
You’ll want to play with the .addItem method of the comboBox and .setEditable(True)
See this page

'''
Configure Cell
'''

# import the java object
import javax.swing.JComboBox as JComboBox

# instantiate a new jBox
comboBox = JComboBox()

# default attributes dictionary
attributes = {'background': 'FFFFFF'}

# only add the renderer attribute to a specific colum
if colName == 'etc':
    attributes['renderer'] = comboBox

# return the attributes dictionary for every cell
return attributes 
    
1 Like

This is awesome but my JComboBox isn’t dropping down… I’m not sure if that’s because I’m unable to activate it, or if it’s drawing inside the bounds of the cell?

Any ideas off-top? Thanks

can you please provide your code.

I used what was above exactly, with the exception that I gave the JComboBox some items ( JComboBox(['foo','bar','baz']) )

Anyway, I guess since I'm necromancing I'll go ahead and post the solution:

If you want the JComboBox to drop down and be selectable, you have to do this in the configureEditor section, which is a bit more tricky. The cell will not look like a dropdown until you give it focus unless you configure the cell and the editor. I personally think it looks nicer when the dropdown only displays on focus

And since I trimmed that up a (tiny) bit, here's a JComboBox power table drop down selectyboi that does scale modes

from javax.swing import AbstractCellEditor,JComboBox
from javax.swing.table import TableCellEditor

class myTableCellEditor( TableCellEditor, AbstractCellEditor ):	

def __init__(self, tableComponent):
	self.table    = tableComponent
	self.comboBox = None
	
def getTableCellEditorComponent(self, table, value, isSelected, rowIndex, vColIndex):

	# make a box. you could do a db query here or whatever
	scaleModes = ['Off', 'Linear', 'Square Root', 'Exponential Filter', 'Bit Inversion']
	self.comboBox = JComboBox(scaleModes)

	# set the currently selected item
	initialVal = self.table.data.getValueAt( rowIndex, 'Scale Mode' )
	self.comboBox.setSelectedItem( initialVal )

	return self.comboBox
		
def getCellEditorValue(self):
	return self.comboBox.getSelectedItem()
	
if colName == 'Scale Mode':
	return {'editor' : myTableCellEditor( self ) }

tl;dr: I should have searched better ¯\_(ツ)_/¯

1 Like