Power Table and Buttons

I have a power table that has several columns, 2 of the columns have a button on in. So far this work as indent, but I would like for one of the buttons to hide after a click, that is where I am getting stuck.

I look for the value 1 in order to display the button, but after the click, it set that value to 0, but the button doesn't go away, even after a refresh on the power table dataset

system.db.refresh(event.source.parent.getComponent('iTable'), "data")

the button do disappear if I click away into another row, but if I click in the cell where the button was, it show up again and runs the code, which I am trying to avoid.

below are the codes I have so far:

  • btn1 (run an update sql query)
  • btn2 (open a pop up window)

sample dataset for the power table:

ibID, Date      , Vendor, Btn1, Btn2
1   , 10/10/2024, test1 ,    1, 1
2   , 10/10/2024, test2 ,    0, 1

the dataset above will make the power table, display 2 buttons on row 1, and only 1 button on row 2.

but if I click on the cell for row 2, btn1, if will un-hide the button

as far the code on the power table:

under Extensions Functions > configureCell

	import java
	from javax.swing import JButton, SwingConstants
	from javax.swing.border import MatteBorder,SoftBevelBorder
	from javax.swing.table import TableCellRenderer
	
	if colName == 'Btn2':
		class MyTableCellRenderer_Btn2(TableCellRenderer):
			def __init__(self):
				super(MyTableCellRenderer_Btn2, self).__init__()
				self.button = JButton("Btn2")
				
			def getTableCellRendererComponent(self, table, value, isSelected, hasFocus, row, column):
				return self.button
				
		r_Btn2 = MyTableCellRenderer_Btn2()
		return { "renderer": r_Btn2.button }
	
	if colName == 'Btn1' and value == 1:
		class MyTableCellRenderer_Btn1(TableCellRenderer):
			def __init__(self):
				super(MyTableCellRenderer_Btn1, self).__init__()
				self.button = JButton("Btn1")
			
			def getTableCellRendererComponent(self, table, value, isSelected, hasFocus, row, column):
				return self.button
			
		r_Btn1 = MyTableCellRenderer_Btn1()
		return { "renderer": r_Btn1.button }
	
	if selected:
		return {'background': self.selectionBackground}
	elif rowView % 2 == 0:
		return {'background': '#1A2430'}
	else:
		return {'background': '#30363c'}

under Extensions Functions > configureEditor

	from javax.swing import AbstractCellEditor, JButton 
	from javax.swing.table import TableCellEditor
	import javax.swing.CellEditor
			
	# load data
	ds = self.getData()

	if colName == "Btn2":
		def func_Btn2(event):
			ibID   = ds.getValueAt(self.selectedRow, 'ibID')
			window = system.nav.openWindow('popup_inbound_edited', {'ibID' : ibID})
			system.nav.centerWindow(window)
			
		class MyCellEditor_Btn2(AbstractCellEditor,TableCellEditor):
			def getCellEditorValue(e):
				return None
			def getTableCellEditorComponent(self,table,value,isSelected,row,column): 
				return JButton("Btn2",actionPerformed=func_Btn2)
				return self.selectedRow
		
		return {'editor':MyCellEditor_Btn2()}
	
	if colName == "Btn1":
		def func_Btn1(event):
			valueID     = ds.getValueAt(self.selectedRow, 'ibID')
			timestamp = system.date.format(system.date.now(), "yyyy-MM-dd HH:mm:ss")
			
			# update database
			result = system.db.runPrepUpdate("UPDATE [table] SET [value1] = ? WHERE [value3] = ?", [timestamp,valueID], 'prod_ignition')

			# refresh table
			self.parent.getComponent('btn_refresh').doClick()

		class MyCellEditor_Btn1(AbstractCellEditor,TableCellEditor):
			def getCellEditorValue(e):
				return None
			def getTableCellEditorComponent(self,table,value,isSelected,row,column):
				return JButton("Btn1",actionPerformed=func_Btn1)
				return self.selectedRow
		
		return {'editor':MyCellEditor_Btn1()}

I am open to suggestion if this is no the right approach.

this is a plus if anyone know ways to customize the button, like background color, foreground color, add icons.

I haven't tested your code at all, but this isn't correct. The first return statement will end the function, and consequently self.selectedRow will never get returned.

I imagine it could be done like this:

getTableCellEditorComponent(self,table,value,isSelected,row,column):
		# Create the JButton, and assign the it to a variable
		button = JButton("Btn1",actionPerformed=func_Btn1)
		
		# Manipulate the button's properties
		button.background = system.gui.color('yellow')
		
		# Then, return the button
		return button

Edit:
Taking a closer look at this, I don't see the need for import java. I recommend deleting that.

Using your code, I was able to color the buttons from the init method of the table cell renderer:

button = JButton("Btn2")
button.background = system.gui.color('yellow')
self.button = button

Result:
image

It would probably be better to use the enabled property. In any case, I imagine that a custom dataset property or a hidden column could be added to the table to store whether or not the button at a given row can be used. The button's action performed script could change the value, and the isCellEditable extension function could read it to ensure the button can't fire again. The configureCell extension function could also read it to set the enabled property for each button.
image

Hello @justinedwards.jle,

thanks for the reply, some of that info you mention I implement and it is working great, like the button customization and removing the import java.

Below are the changes, case anyone else want see configureCell the following:

if colName == 'Btn2':
	class MyTableCellRenderer_DEP(TableCellRenderer):
		def __init__(self):
			super(MyTableCellRenderer_Btn2, self).__init__()
			
			button = JButton("Btn2")
			if value == 1:
				button.background = system.gui.color('yellow')
				button.enabled = 1
			else:
				button.background = system.gui.color('gray')
				button.enabled = 0
			self.button = button
			#self.button = JButton("Btn2")
		
		def getTableCellRendererComponent(self, table, value, isSelected, hasFocus, row, column):
			return self.button
	
	r_DEP = MyTableCellRenderer_Btn2()
	return { "renderer": r_Btn2.button }

this does disable the button, but I still running into a issue.

Since the configureEditor is active, anytime I click the cell, the end user see a button getting press. I am trying to disable that for an end user experience.

The code in configureEditor script still executed every time a mouse click in the cell. I just add a check for the value, but I can only get that value when I click the cell, do you know a way to check for that before hand?

below is the code in the configureEditor

if colName == "Btn2":
	def func_Btn2(event):
		ds = self.getData()
		btnStatus = ds.getValueAt(self.selectedRow, 'Btn2')
		
		if btnStatus == 1:
			print "RUN CODE"
		else:
			print "DO NOTHING"
		
	class MyCellEditor_Btn2(AbstractCellEditor,TableCellEditor):
		def getCellEditorValue(e):
			return None
		def getTableCellEditorComponent(self,table,value,isSelected,row,column): 
			return JButton("Btn2",actionPerformed=func_Btn2)
	
	return {'editor':MyCellEditor_Btn2()}

For each button. add a corresponding boolean value to a hidden column in the dataset, or a custom property dataset, then have the isCellEditable extension function check the stored values for each cell returning True or False accordingly. This way, a disabled button will also be uneditable, and the user will not be able to access the button when clicking the cell