Skip to content

Instantly share code, notes, and snippets.

@AravindaM
Last active June 27, 2016 02:42
Show Gist options
  • Save AravindaM/01bd01259c550f852b9e to your computer and use it in GitHub Desktop.
Save AravindaM/01bd01259c550f852b9e to your computer and use it in GitHub Desktop.
Change JTable Background / Foregorund Colour
import java.awt.Color;
import java.awt.Component;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class CustomJTable extends JTable {
Color backgroundColor = getBackground();
Color foregroundColor = getForeground();
@Override
public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
int columnIndex) {
JComponent component = (JComponent) super.prepareRenderer(renderer, rowIndex, columnIndex);
if(columnIndex == 4){
if(getValueAt(rowIndex, 4).toString().equalsIgnoreCase("0")) {
component.setBackground(Color.GREEN.darker());
component.setForeground(Color.GREEN.darker());
}else {
component.setBackground(Color.GRAY.brighter());
component.setForeground(Color.GRAY.brighter());
}
}else{
component.setBackground(backgroundColor);
component.setForeground(foregroundColor);
}
return component;
}
}
@AravindaM
Copy link
Author

This code changes the Fifth column background co lour to Green if the Cell value is 0, otherwise it will be gray.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment