Getting text of JTable cells via cell renderer

Last edited on

Getting text of JTable cells is sometimes not directly possible. In such cases the Java API can be used directly inside of the Squish test scripts:

def main():
    #...

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

    text = get_jtable_cell_text(jtable, 0, 0)
    test.log("Cell: 0/0: %s" % text)

def get_jtable_cell_text(jtable, row, column):
    component = get_jtable_cell_component(jtable, row, column)
    return component.text

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

Related Information