Getting foreground, background color of JTable cells

Last edited on

To retrieve the foreground or background color of JTable cells the Java API can be used directly inside of the Squish test scripts:

def main():
    startApplication("my_aut")

    #...
    jtable = waitForObject({"type": "javax.swing.JTable"})

    # This is an instance of javax.swing.plaf.ColorUIResource
    # https://docs.oracle.com/javase/8/docs/api/javax/swing/plaf/ColorUIResource.html
    background = get_jtable_cell_background(jtable, 0, 0)

    test.compare(255, background.getRed())

def get_jtable_cell_foreground(jtable, row, column):
    component = get_jtable_cell_component(jtable, row, column)
    return component.getForeground()

def get_jtable_cell_background(jtable, row, column):
    component = get_jtable_cell_component(jtable, row, column)
    return component.getBackground()

def get_jtable_cell_component(jtable, row, column):
    renderer = jtable.getCellRenderer(row, column);
    value = jtable.getModel().getValueAt(row, column)
    selectedColor = jtable.getSelectionModel().isSelectedIndex(row)
    hasFocus = True
    component = renderer.getTableCellRendererComponent(jtable, value, selectedColor, hasFocus, row, column)
    return component
test.py

Related Information