private JTable table;
private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
public TablePrepareRenderer() {
Object[] columnNames = { "Type", "Company", "Name", "Salery", "Designation" };
Object[][] data =
{ { "Probation", "Digital Research Lab", "Kamran Ali", "500,000", "Java Developer" }, { "Permenent", "Netsole",
"Farhan Khan",
"80,000",
"System Administaror" },
{ "Contract", "System Limited", "Danyal", "100,000", "Network Administrator" },
{ "Probation", "TeraData", "Ali Raza", "45,000", "IT Officer" },
{ "Contract", "MicroSoft", "Sikandar Hayat", "450,000", "Team Lead" },
{ "Permenent", "MicroSoft", "Adnan", "30,000", "Driver" }, };
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table = new JTable(model) {
private static final long serialVersionUID = 1L;
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (!isRowSelected(row)) {
if (table.getColumnCount() >= 0) {
String type = (String)getModel().getValueAt(row, 0);
if (type.equalsIgnoreCase("Probation")) {
c.setBackground(new Color(198, 190, 255));
}
if (type.equalsIgnoreCase("Permenent")) {
c.setBackground(new Color(14, 255, 190));
}
if (type.equalsIgnoreCase("Contract")) {
c.setBackground(Color.green);
}
}
}
if (isRowSelected(row) && isColumnSelected(column)) {
((JComponent)c).setBorder(new LineBorder(Color.red));
}
return c;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TablePrepareRenderer frame = new TablePrepareRenderer();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setSize(800, 500);
frame.setVisible(true);
}
});
}
}