Embedding JSpinner to table column

Has anyone tried to embedd a JSpinner to the Power Table component?
Having some problems understanding where to add what in the extension functions.

I will post a generic design pattern for implementing your own editors.

Step #1
Create a project or shared script where you define what you want your editor to do.

from javax.swing.table import TableCellEditor
from javax.swing import JSpinner,SpinnerNumberModel,AbstractCellEditor

class TableNumberSpinner(AbstractCellEditor,TableCellEditor):
	comp = JSpinner()
	
	def __init__(self):
		self.comp.setModel(SpinnerNumberModel(0,0,100,1))
		self.comp.setEditor(JSpinner.NumberEditor(self.comp))

	def getTableCellEditorComponent(self,table, value, isSelected, rowIndex, vColIndex):
		self.comp.setValue(value)
		return self.comp
	
	def getCellEditorValue(self):
		return self.comp.getValue()

Ok. The first 2 lines define what we are importing to create our editor.

Line 4 is the start of our class. You will notice that there are 2 classes defined in the declaration. Essentially, any method call will be searched for in the first class (AbstractCellEditor) and its parents. If it fails to find, it searches the next class/interface, until the list is exhausted. Essentially, start with the class you want to “extend”, followed by any interfaces.

Line 5 is defining the base component used for our editor.

def init(self) is our constructor. This is where we set out initial component settings. Here, I am using a SpinnerNumberModel to define the model for the spinner (Default value of 0, range of 0-100, imcremented by 1) and setting the editor for the spinner to be numeric

def getTableCellEditorComponent is called when the cell is getting edited. Essentially, when you click the cell. Here, we are setting the value for the editor, and returning it. This can get more complex, such as setting available options (For a drop down, as an example), but for this example, we will keep it east like this.

def getCellEditorValue is called when the cell is done being edited (when the cell loses focus, or you hit enter).

Step #2
Now we have to have the table use the editor, to do this, open the configureEditor extension function.

	if colName == "Int Column":
		return {"editor":shared.table.TableNumberSpinner()}

Essentially, we are telling the function to use the class to use when the colName is “Int Column”. You will have to modify to suit.

Any questions, please let me know.

2 Likes

Thank you for the explaination! :slight_smile: is it also possible to add a renderer, so that the JSpinner is displayed without having to press the cell first?

This thread was among the results of a search for "renderer" (-:

Place this in configure cell. It is a little slow, but it works nonetheless.

from javax.swing import JSpinner,SpinnerNumberModel

if colName == "Int Column":
	return {"renderer":JSpinner(model=SpinnerNumberModel(value,0,100,1))}