I make a table with multiline cell, it worked fine, but I want to change the color of a single row after making specific colomns muliline cell, not the color of the whole colomn. How can i do that?
here is an image after making colomn 2 multiline cell, but the whole colomn become white
this is what i did :
jTable1.getColumnModel().getColumn(1).setCellRenderer( new TextAreaRenderer2());
String test = "sunday \n monday ";
jTable1.getModel().setValueAt(test, 0, 3);
jTable1.getModel().setValueAt(test, 0, 1);
jTable1.getModel().setValueAt(test, 0, 2);
and this is TextAreaRenderer2 class :
public class TextAreaRenderer2 extends JTextArea
implements TableCellRenderer {
public TextAreaRenderer2() {
Font font = new Font("Aparajita", Font.BOLD + Font.ITALIC, 16);
setLineWrap(true);
setWrapStyleWord(true);
setBackground(Color.yellow);
setBorder(BorderFactory.createEmptyBorder());
setFont(font);
}
@Override
public Component getTableCellRendererComponent(JTable jTable,
Object obj, boolean isSelected, boolean hasFocus, int row,
int column) {
setText((String)obj);
setBackground(Color.WHITE);
return this;
}
}
In your TableAreaRenderer2
class, in the getTableCellRendererComponent
method you should set the background color based on the row. Something like this:
if (row % 6 < 3) {
setBackground(Color.LIGHT_GRAY);
} else {
setBackground(Color.WHITE);
}
The above code will make 3 rows light gray the next 3 white and repeat the same pattern.