asked on
occupationTable = new JTable(model);
occupationTable.setDefaultRenderer(Object.class, new tableColour());
package renderers;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import dataTypes.OccupationTableModel;
public class tableColour extends JTable implements TableCellRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
// public static final DefaultTableCellRenderer DEFAULT_RENDERER = new
// DefaultTableCellRenderer();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
setOpaque(true);
OccupationTableModel aModel = (OccupationTableModel) table.getModel();
String Acceptence = aModel.getOccupation(rowIndex).getgeneralAcceptence();
if (isSelected) {
this.setForeground(Color.BLACK);
this.setBackground(Color.BLUE);
} else {
if (Acceptence != null) {
try {
if (Acceptence.equals("Decline")) {
this.setForeground(Color.BLACK);
this.setBackground(Color.RED);
} else {
this.setForeground(Color.BLACK);
this.setBackground(Color.WHITE);
}
} catch (Exception e) {
System.out.println("Shit");
e.printStackTrace();
}
}
}
return this;
}
}
UnformattedTable.jpgJava is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
I'd rather suggest to extend DefaultTableCellRenderer and overwrite getCompoenet methodadding colors there