Last active
June 27, 2016 02:42
-
-
Save AravindaM/01bd01259c550f852b9e to your computer and use it in GitHub Desktop.
Change JTable Background / Foregorund Colour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code changes the Fifth column background co lour to Green if the Cell value is 0, otherwise it will be gray.