I want a button when clicked, calls upon a method that changes a jTable
object's cell colors. Something like:
jTableName.setCellBackground(Color, row, column)
Bringing me to a method as close or similar to this would really help me right now.
This might help you
You can use DefaultTableCellRenderer to color alternate row from JTable.
table.setDefaultRenderer(Object.class, new TableCellRenderer(){ private DefaultTableCellRenderer DEFAULT_RENDERER = new >DefaultTableCellRenderer();
@Override public Component getTableCellRendererComponent(JTable table, Object >value, boolean isSelected, boolean hasFocus, int row, int column) { Component c = >DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, >hasFocus, row, column); if(isSelected){ c.setBackground(Color.YELLOW); }else{ if (row%2 == 0){ c.setBackground(Color.WHITE); } else { c.setBackground(Color.LIGHT_GRAY); } } //Add below code here return c; } });
If you want to color your row using the value of a particular row then you can >use something like this. Add these line to above
if(table.getColumnModel().getColumn(column).getIdentifier().equals("Status")){//Here
Statusis column name if(value.toString().equals("OK")){//Here
OKis the value of row
c.setBackground(Color.GREEN);
}
This is from here