Hi,
I would like to display a list of parameters in a power table with 2 columns : Title and Value.
The values have different types, each has a render type and editor type :
- boolean : JCheckbox
- interger : JComboBox
- string : JTextField
I manage to get the desired render with the "configureCell" function
However i can't get the same result with the "configureEditor" Function. It only allows to use one editor per column.
I guess it the event behind :
"jTable.getColumn("xx").setCellEditor(new DefaultCellEditor())"
I found an exemple on the web but i couldn't implement it.
Each Row with different Editor Example
Can someone please help me !!
Thank you.
Maxime.
Do you really need a table for this?
Perhaps you can come a long way with the template repeater. Just make a template that has a label on the left, and various input fields on the right. Show only the correct input field based on the data type, and make sure to pass everything via template parameters.
To get the values back, you can write it to a memory tag.
By adding borders, you can make it look like a real table. There are only a few things you’re missing, like f.e. resizing columns.
This is an example of what I made with the template repeater. The keys are for a label, and are extracted from a label template. The values are user-editable.

Maybe you are right. I hesitated with this solution.
However, I managed to do something nice with a power table.
I only have one problem left : i Have None in “newValue” in event “onCellEdit”.
My code for “configEditor”
from javax.swing import DefaultCellEditor
from javax.swing import DefaultComboBoxModel
from javax.swing import JTextField
from javax.swing import JComboBox
from javax.swing import JCheckBox
if colName == "val":
root = self
class CustomCellEditor(DefaultCellEditor):
def getTableCellEditorComponent(self, table, value, isSelected, row, column):
#get powertable.data
pydata = system.dataset.toPyDataSet(root.data)
#get type (in another column)
type = pydata[row]["type"]
#get column value
val = pydata[row]["val"]
#type boolean
if type == "boolean":
#cast string in bool
checkboxValue = val in ['true', '1', 'True']
dce = DefaultCellEditor(JCheckBox())
comp = dce.getTableCellEditorComponent(table, value, isSelected, row, column)
# set selected value
comp.setSelected(checkboxValue)
return comp
#type combo box
elif type == "combo":
dce = DefaultCellEditor(JComboBox())
comp = dce.getTableCellEditorComponent(table, value, isSelected, row, column)
dataFullText = []
#get powertable.options (custom property of power table, type : dataset, list of combobox options)
data = []
...
#code for make list of combobox options
...
comp.setModel(DefaultComboBoxModel(data))
selectedText = ""
...
#code for get item to select
...
#set selected value
comp.setSelectedItem(selectedText)
return comp
retour = CustomCellEditor(JComboBox())
return {'editor': retour}
I tried to rewrite the property getCellEditorValue () but without success.
I have an error : “getCellEditorValue() takes no arguments (1 given)”.
I think that in python there is a first argument, even if it is not declared : “self”
With “@staticmethod” it’s work better !!!
I managed to do what I wanted. I’m so happy !!
I’m trying to do exactly the same. Where are you putting @staticmethod to get newValue to return the edited value?
Finally I used a repeater template as sanderd17 said. It’s simpler, no need to master Java and more flexible.
It’s heavy and not perfect. There are problems during the validation, etc …
Otherwise here is the code with the static function:
from javax.swing import DefaultCellEditor
from javax.swing import DefaultComboBoxModel
from javax.swing import JTextField
from javax.swing import JComboBox
from javax.swing import JCheckBox
from javax.swing import JSpinner
from javax.swing import SpinnerDateModel
from java.util import Calendar
from java.util import Date
from javax.swing.BorderFactory import createEmptyBorder
#on test si on est sur la colonne "val"
if colName == "val":
#copie self dans une autre variable pour éviter la confusion.
root = self
#crée une classe hérité de DefaultCellEditor
class CustomCellEditor(DefaultCellEditor):
#On stock les pointeurs pour quand on va vouloir récupérer les valeurs.....
#myJComboBox = CustomJComboBox()
myJComboBox = JComboBox()
myJCheckBox = JCheckBox()
myJTextField = JTextField()
#myJSpinner = JSpinner()
#défini le nombre de clic pour editer la cellule....
@staticmethod
def isCellEditable(event):
#test si double clic ou supérieur
if event.getClickCount() >= 2:
#si oui alors on edite
return True
else:
#si non on édite pas.
return False
#méthode static sinon ca marche pas.
@staticmethod
def getCellEditorValue(): #on réécrit la méthode qui permet de récupérer la valeur d'édition.....(°°)
result = "erreur"
try:
#on essaye de récupérer la
if CustomCellEditor.myJComboBox is not None:
result = CustomCellEditor.myJComboBox.getSelectedItem()
elif CustomCellEditor.myJCheckBox is not None:
result = CustomCellEditor.myJCheckBox.isSelected()
elif CustomCellEditor.myJTextField is not None:
result = CustomCellEditor.myJTextField.getText()
elif CustomCellEditor.myJSpinner is not None:
result = CustomCellEditor.myJSpinner.getValue();
else:
result = "rien de trouver"
except Exception, e: print(e)
#on renvoi la valeur récupérer : ouf !!!
return result
#redéfini la méthode "getTableCellEditorComponent"
def getTableCellEditorComponent(self, table, value, isSelected, row, column):
#on récupére le dataset
pydata = system.dataset.toPyDataSet(root.data)
#on récupère le type sous forme de string
type = pydata[row]["type"]
#on récupére la valeur
val = pydata[row]["val"]
#type boolean
if type == "boolean":
#récupère la valeur actuelle
checkboxValue = val in ['true', '1', 'True']
#init default cell editor of type JCheckBox()
dce = DefaultCellEditor(JCheckBox())
#recupère le pointeur vers getTableCellEditorComponent
comp = dce.getTableCellEditorComponent(table, value, isSelected, row, column)
#set selected value
comp.setSelected(checkboxValue)
#On set le fond à la même couleur que le reste de la ligne si sélectionné.
comp.setBackground(root.selectionBackground)
#on sauvegarde un pointeur vers cet objet pour en récupérer la valeur..... ca peut grave servir !!!!!
CustomCellEditor.myJCheckBox = comp
CustomCellEditor.myJComboBox = None
CustomCellEditor.myJTextField = None
#CustomCellEditor.myJSpinner = None
#on renvoi le tout.
return comp
#Type text field
elif type == "string":
#récupère la valeur actuelle
textValue = val
#init default cell editor of type JCheckBox()
dce = DefaultCellEditor(JTextField())
#print dce
#recupère le pointeur vers getTableCellEditorComponent
comp = dce.getTableCellEditorComponent(table, value, isSelected, row, column)
#set selected value
comp.setText(textValue)
#On ne set pas le fond car pas lisible
#comp.setBackground(root.selectionBackground)
#on sauvegarde un pointeur vers cet objet pour en récupérer la valeur..... ca peut grave servir !!!!!
CustomCellEditor.myJCheckBox = None
CustomCellEditor.myJComboBox = None
CustomCellEditor.myJTextField = comp
#CustomCellEditor.myJSpinner = None
#on renvoi le tout.
return comp
#type combo box
elif type == "combo":
#init default cell editor of type JComboBox()
dce = DefaultCellEditor(JComboBox())
#recupère le pointeur vers getTableCellEditorComponent
comp = dce.getTableCellEditorComponent(table, value, isSelected, row, column)
#init variable
dataFullText = []
#récupère pointeur vers dataset options (custom property objet power table)
target = root.options
#on récupère la liste à afficher sous forme de dataset mais sans les valeurs avant le signe =
data = []
#on récupère la liste pour la paramètre choisi
ListText = root.ComboListText(target,root.data.getValueAt(row, 3))
#on supprime les valeurs avant le signe égal.
data = root.ComboListTextLight(ListText)
#On set le model avec la nouvelle liste de donnée à afficher
comp.setModel(DefaultComboBoxModel(data))
#On récupère la valeur à sélectionner
selectedText = root.ComboTextFromInt(ListText, int(val))
#set selected value
comp.setSelectedItem(selectedText)
#Set border
comp.setBorder(createEmptyBorder())
#on sauvegarde un pointeur vers cet objet pour en récupérer la valeur..... ca peut grave servir !!!!!
CustomCellEditor.myJComboBox = comp
CustomCellEditor.myJCheckBox = None
CustomCellEditor.myJTextField = None
#CustomCellEditor.myJSpinner = None
#on renvoi le tout.
return comp
retour = CustomCellEditor(JComboBox())
return {'editor': retour}
1 Like
That worked a treat! Merci beaucoup.