1
使用DefaultTableCellRenderer我編寫此代碼用於插入圖像插入到柱如何插入圖像插入在Java
LessonTable.setDefaultRenderer(Object.class, new SimpleTableCellRenderer());
class SimpleTableCellRenderer extends DefaultTableCellRenderer {
ImageIcon image = new ImageIcon(getClass().getResource("imagefolder/this.png"));
JLabel lbl = new JLabel();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
lbl = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (column == 1) {// i want to set image only for this column
lbl.setHorizontalAlignment(SwingConstants.LEFT);
lbl.setIcon(image);
}
else {
lbl.setHorizontalAlignment(SwingConstants.CENTER);
}
return lbl;
}
}
但這代碼插入該圖像獲得除了第二列中的所有列的列的表。 這裏有什麼問題?