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.