below is the correct image rendering class.
class SimpleCellRenderer extends DefaultTableCellRenderer{ @Override 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); ((JLabel)cell).setIcon((Icon)value); ((JLabel)cell).setText(""); ((JLabel)cell).setHorizontalAlignment(JLabel.CENTER); if (isSelected) { cell.setBackground(Color.white); } else { cell.setBackground(null); }
below is a method from which everything is automatically populated. private void formWindowOpened (java.awt.event.WindowEvent evt)
{ // TODO add your handling code here: fillIcon(); } public void fillIcon() { int i,j,rowValue,colValue; int cols= student.getColumnCount(); int rows=student.getRowCount(); for(i =0 ;i<rows ;i++) { for(j=3; j<cols;j++) { rowValue = i; colValue = j; String value = (String)student.getValueAt(rowValue, colValue); if(value.equals("h"))//here h is the value stored in your database which is used to set some icon in place of value h. { ImageIcon icon = new ImageIcon(getClass().getResource("dash.png")); student.setValueAt(icon, rowValue, colValue); student.getColumnModel().getColumn(colValue).setCellRenderer(new SimpleCellRenderer()); }
source share