i'm setting up a custom cell renderer for a JTable, that has to contain an album cover image, that is from a byte[] taken with another library, the problem is that converting to a BufferedImage is so slow that in earlier version of the code the program wouldn't even start. Now the situation is better, but the lag is very noticeable nonetheless. Here is the code:
public class ImageCellRenderer extends DefaultTableCellRenderer {
JLabel lbl = new JLabel();
public Image[] getAlbumart() throws IOException {
Image[] albumArt = new Image[allmymusic.size()];
for (int i = 0; i < allmymusic.size(); i++) {
byte[] data = allmymusic.get(i).albumImageData;
ImageIO.setUseCache(false);
if(allmymusic.get(i).albumImageData != null){
BufferedImage bImage;
try(ByteArrayInputStream bis = new ByteArrayInputStream(data)) {
bImage = ImageIO.read(bis);
}
ImageIcon icon = new ImageIcon(bImage);
Image image = icon.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH);
// ImageIcon iconscaled = new ImageIcon(image);
albumArt[i] = image;
} else {
albumArt[i] = null;
}
}
return albumArt ;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
ImageIcon icon = new
ImageIcon(getClass().getResource("/images/defaultimage.png"));
Image image = icon.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH);
Image[] albumArt;
try {
albumArt = getAlbumart();
if(albumArt[row] == null){
lbl.setIcon(new ImageIcon(image));
} else {
lbl.setIcon(new ImageIcon(albumArt[row]));
}
} catch (IOException ex) {
Logger.getLogger(ImageCellRenderer.class.getName())
.log(Level.SEVERE, null, ex);
}
return lbl;
}
}
I accept alternative solutions.