I am moving a Java/Swing app to Kotlin and have run into an issue with JTable. I wish to change the background colour of a cell. The way to do this in Java is to override the DefaultTableCellRenderer function. This is the java code:
private class PriceRenderer extends DefaultTableCellRenderer {
private static final Border SELECTED_BORDER = new LineBorder(Color.BLUE, 1);
private static final Border NO_FOCUS_BORDER = new LineBorder(new Color(0, 0, 0, 0), 1); @Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
int modRow=tableObj.convertRowIndexToModel(row);
String ticker = (String) ((SecTableModel) table.getModel()).getValueAt(modRow, 1);
Integer status = dm.getTickerStatus(ticker);
if (status == null)
status = 0;
switch (status) {
case Constants.TASKSTARTED:
setOpaque(true);
setForeground(Color.BLACK);
setBackground(Color.YELLOW);
break;
case Constants.TASKFAILED:
setOpaque(true);
setForeground(Color.BLACK);
setBackground(Color.RED);
break;
case Constants.TASKCOMPLETED:
setOpaque(true);
setForeground(Color.BLACK);
setBackground(Color.GREEN);
break;
default:
setOpaque(true);
setForeground(UIManager.getColor("TextField.Foreground"));
Color colour = (row %2 ==0 ? colors.registerBG2:colors.registerBG1);
setBackground(colour);
}
if (isSelected) {
setBorder(SELECTED_BORDER);
setOpaque(false);
} else {
setBorder(NO_FOCUS_BORDER);
setOpaque(true);
}
setHorizontalAlignment(JLabel.RIGHT);
return this;
}
}
Though this can be converted to Kotlin the problem is the call to super.getTableCellRendererComponent which is required to get the appropriate cell. This obviously doesn’t work under Kotlin as the super. call is not valid>
Has anyone encountered this issue and found an answer? My only recourse seems to be is have a Java class in my app.
Thanks
Mike