How to make a JTable Column changes it color According to Specific Values from the Database
I'm Building a Client/server Desktop Application.
I wanted to know if it is possible to make a JTable Column Changes 开发者_Go百科its color according to specific values from the database (oracle).
- Red= "Refused".
- Green="Accepted".
- yellow="Quarantine".
Use an appropriate renderer.
Use a custom column renderer for this:
public class ColorColumnRenderer extends DefaultTableCellRenderer
{
Color bkgndColor, fgndColor;
public ColorColumnRenderer(Color bkgnd, Color foregnd) {
super();
bkgndColor = bkgnd;
fgndColor = foregnd;
}
public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
cell.setBackground( bkgndColor );
cell.setForeground( fgndColor );
return cell;
}
}
You can use that renderer like this (example for a single column):
TableColumn tm = table.getColumnModel().getColumn(0);
tm.setCellRenderer(new ColorColumnRenderer(Color.lightGray, Color.blue));
Once you have the color changing code you can create a listener which listens for the value changes to the table model and by checking the changes you can apply the colors you want to specific columns.
精彩评论