Power Table custom renderer with JButton

Hello,

I am trying to add a button to a column in a power table. I am able to add the button and register an action listener, but the clicking event is never received by the listener. Any suggestions?

In the Power Table “configureCell” extension method I have:

[code] if colName == “Action”:
import java
from javax.swing import JButton
from javax.swing.table import TableCellRenderer

	class MyTableCellRenderer(TableCellRenderer):
		def __init__(self):
			super(MyTableCellRenderer, self).__init__()
			self.button = JButton("test")
			
		def getTableCellRendererComponent(self, table, value, isSelected, hasFocus, row, column):
			return self.button
	        
	r = MyTableCellRenderer()
	return { "renderer": r.button }[/code]

In the Power Table “configureEditor” extension method I have:

[code] if colName == “Action”:
import java
from java.awt.event import ActionListener
from javax.swing import JButton
from javax.swing.table import TableCellEditor

	class MyTableCellEditor(TableCellEditor):
		def __init__(self):
			super(MyTableCellEditor, self).__init__()
			self.button = JButton("test")
			
			class ActionListener1(ActionListener):
				def actionPerformed(self, event):
					print "clicked!"
			
			al1 = ActionListener1()
			self.button.addActionListener(al1)
			
		def getCellEditorValue():
			return None
		
		def getTableCellEditorComponent(self, table, value, isSelected, row, column):
			return self.button
	        
	e = MyTableCellEditor()
	return { "editor": e }[/code]

Any suggestions?

The goal is to add several buttons to every row in the power table to enable actions such as “open link”, “delete row”, etc.

I am using separate columns with an icon to achieve this result currently, but it is not ideal.

Can you use the onMousePressed extension function? You’ll have the rowIndex and colIndex/colName variables available.

Unfortunately this does not provide enough information to determine what button was pressed within a single cell. I am using the double click extension method at this point to pick up on a click event anywhere within the cell, but this means that I can only have one button (or image) per cell.

I think I will be able to solve this another way however, perhaps by using a template repeater.

I have the same request as in this thread. I found a similar thing in the Ignition Demo- OEE Downtime table. (see attached screenshot)
I tried to trace the code in the given project but did not find anything.
Any ideas how to implement this?

Hi Guys,

I have the exact solution for you.

It is not enough to implement the TableCellEditor interface. You must also extend/subclass the AbstractCellEditor class at the same time.

The reason is because AbstractCellEditor implements the CellEditor interface which needs to be implemented in order for general editors/components, such as the JButton, to work.

Here is a simple configureEditor example that works!:if colName == "String Column": from javax.swing import AbstractCellEditor, JButton from javax.swing.table import TableCellEditor def func(e): print "This works. My Test." class MyCellEditor(AbstractCellEditor,TableCellEditor): def getTableCellEditorComponent(self,table,value,isSelected,row,column): return JButton("My Button",actionPerformed=func) return {'editor':MyCellEditor()}Also make sure that your column is set as Editable in the Table Customizer.

Best,

2 Likes

That worked great, but how do I pass in the selected row or any other properties from the user?

Nevermind - I forgot about the self attributes. I can use the self.selectedRow for example.

Yes, correct.

Can I used a Toggle Button Component instead of Button Component???

Nice, it works! Thank you so much!