Java – refresh JTable when setautocreaterowsorter is true
•
Java
I wrote a default table as follows:
public class CustTableRenderer extends DefaultTableCellRenderer{ @Override public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int col) { Component comp = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col); try { Object cellObj = table.getModel().getValueAt(row,7); double cellValue = (Double) cellObj; if (cellValue < 0) { comp.setBackground(new Color(255,48,48)); } else if (cellValue == 0) { comp.setBackground(new Color(173,255,47)); } else { comp.setBackground(Color.white); } if (isSelected) { comp.setBackground(new Color(71,60,139)); TableModel model = table.getModel(); } } catch (Exception e) { e.printStackTrace(); } return comp; } }
To highlight the row containing column 7 impairment, I also set setautocreaterowsorter to true My problem is that when I click a heading to sort, the table is sorted, but the highlighted rows have not changed, so the wrong rows are highlighted
How do I redraw tables when sorting?
Solution
Coordinates passed to the renderer in the view coordinate system, you must convert them to model coordinates before accessing the model:
int modelRow = table.convertRowIndexToModel(row); int modelColumn = table.convertColumnIndexToModel(column); cellObject = table.getModel().getValueAt(modelRow,modelColumn);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码