Estou com dúvidas de como usar o DefaultTableCellRenderer na minha aplicação para mudar as cores das linhas…
Ai está minha classe
public class ResultSetTableModel extends AbstractTableModel {
private static class Column {
public final Class<?> CLASS;
public Column(final String name, final Class<?> type) {
CLASS = type;
}
}
private static class Row {
public final Object[] VALUES;
public Row(final ResultSet rs) throws SQLException {
final int columns = rs.getMetaData().getColumnCount();
VALUES = new Object[columns];
for (int i = 1; i <= columns; i++) {
VALUES[i - 1] = rs.getObject(i);
}
}
}
private static final long serialVersionUID = 1L;
private List<Column> columns;
private List<Row> lines;
public ResultSetTableModel(final ResultSet rs) throws SQLException,
ClassNotFoundException {
columns = new ArrayList<Column>();
final ResultSetMetaData md = rs.getMetaData();
final int count = md.getColumnCount();
for (int i = 1; i <= count; i++) {
columns.add(new Column(md.getColumnName(i), Class.forName(md
.getColumnClassName(i))));
}
lines = new ArrayList<Row>();
while (rs.next()) {
lines.add(new Row(rs));
}
}
@Override
public Class<?> getColumnClass(final int columnIndex) {
return columns.get(columnIndex).CLASS;
}
public int getColumnCount() {
return columns.size();
}
@Override
public String getColumnName(final int column) {
switch (column) {
case 0:
return "Nome";
case 1:
return "Login";
case 2:
return "Unidade";
case 3:
return "Cargo";
case 4:
return "Ultimo Acesso";
case 5:
return "Pode Atender";
case 6:
return "Logado";
}
return null;
}
public int getRowCount() {
return lines.size();
}
public Object getValueAt(final int rowIndex, final int columnIndex) {
return lines.get(rowIndex).VALUES[columnIndex];
}
@Override
public boolean isCellEditable(final int rowIndex, final int columnIndex) {
return false;
}
}
e a outra classe
public static void main(String[] a) {
final String SELECT_USUARIO = "SELECT nome_usu, login_usu, nm_uni, nm_cargo, ult_acesso, pode_atender, logado FROM view_usuarios WHERE cod_uni = '3' and ( nm_cargo = 'Atendente' or nm_cargo = 'Triagem')";
final JFrame frame = new JFrame("Monitoramento");
final JTable table = new JTable();
try {
Class.forName("org.postgresql.Driver").newInstance();
Connection conn = DriverManager.getConnection(
"jdbc:postgresql://10.23.220.95:5432/sga", "postgres",
"postgres");
PreparedStatement ps = conn.prepareStatement(SELECT_USUARIO);
ResultSet rs = ps.executeQuery();
// table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setModel(new ResultSetTableModel(rs));
table.getColumnModel().getColumn(0).setPreferredWidth(250);
table.getColumnModel().getColumn(2).setPreferredWidth(250);
table.getColumnModel().getColumn(4).setPreferredWidth(100);
table.getColumnModel().getColumn(5).setPreferredWidth(100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1100, 300);
frame.setLocationRelativeTo(null);
frame.add(BorderLayout.CENTER, new JScrollPane(table));
frame.setVisible(true);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
o sistema está buscando os dados do banco e na ultima coluna que está LOGADO eu queria mudar e colocar vermelho caso seja false e verde caso seja true.