Ok so this has stumped me for a few days. Maybe the title isn't accurate enough but it's the only thing i could think of to describe my situation.
My end goal for the user is that when they edit a row, only in column 4 or 5, will the table highlight (set background color yellow) any rows where the data matches the values in the editing row of column 4 and 5 respectively, EXCEPT for the actual editing row. (both of these columns are jcomboboxes)
sounds good? well i'm also trying to keep those rows highlighted, while when the user edits another row in column 4 or 5 with different values from before, still repeat and highlight the matching rows, while not re-rendering the previously highlighted rows. its proving to be very difficult for me because i dont quite understand whats going on.
eventually, when i figure this out, the way the user will remove the color from the row (signifying they have checked that data) by simply selecting the row.
i need to know how the renderer is called in a jtable. it seems to be called every time a change is made. is there a way to render the table, then bypass the call to the renderer so that it doesnt have to constantly re draw the cells? i don't know if i'm asking the right questions.
i'm trying to override the getTableCellRendererComponent method and return the color that way but when i edit a different cell, i lose what i had highlighted from the first edit. and whats highlighted isnt all that correct either, it gets most of the matching data plus other rows that do not match. i don't fully understand the renderer i guess
there has to be a concept i'm not grasping. or maybe theres a completely different way to do it. i could use a push in the right direction!
public class ColorChange extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (column != 7) {
this.setHorizontalAlignment(SwingConstants.CENTER);
}
else {
this.setHorizontalAlignment(SwingConstants.LEFT);
}
Color curColor = c.getBackground();
if (ColorCheck(table, row, column)) {
c.setBackground(Color.YELLOW);
}
else if (curColor == Color.YELLOW) {
c.setBackground(curColor);
}
else {
c.setBackground(Color.WHITE);
}
return c;
}
}
public boolean ColorCheck(JTable jt, int row, int col) {
boolean result = false;
int er = jt.getEditingRow();
int ec = jt.getEditingColumn();
if (er<0 || ec<0) {
return result;
}
String edMainCat = (String) jt.getValueAt(er, 4);
String edSubCat = (String) jt.getValueAt(er, 5);
String MainC = (String) jt.getValueAt(row, 4);
String SubC = (String) jt.getValueAt(row, 5);
if (edMainCat == null || edSubCat == null || MainC == null || SubC == null || row == er) {
return result;
}
if (edMainCat.equals(MainC) && edSubCat.equals(SubC)) {
result = true;
}
return result;
}
nullbefore you call the super implementation (insert the linesetBackground(null);as the first line of you methodgetTableCellRendererComponent). When this hint don't solve your problem, please provide a minimal reproducible example as I've requested above. - Sergiy Medvynskyy