I am working on Java the Application of Swing and which i am getting Data and i am using swing Jtable Render for render Image in that but when data is more its hanging all time so what can i do to prevent that?
example render that i am using.
public class DefaultTableCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
JLabel jLabel;
public DefaultTableCellRenderer() {
jLabel = new JLabel();
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean selected, boolean focus, int row, int col) {
try {
if (row == 1) {
jLabel.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("blank.png"))));
jLabel.setText("Image Data");
jLabel.setBackground(Color.LIGHT_GRAY);
} else {
jLabel.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("blank.png"))));
jLabel.setText("Final");
}
//jLabel.setIcon(new ImageIcon(ImageIO.read(new File("blank"))));
return jLabel;
} catch (Exception e) {
e.printStackTrace();
}
return jLabel;
}
@Override
public boolean mouseEnter(Event evt, int x, int y) {
System.out.println(jLabel.getText());
return true;
}
}
Some suggestions for you:
doInBackground()
. When some images are loaded you can use the methodspublish()
/process()
to update your table model with new images.Executors
indoInBackground()
method.I think, your problem is not the CPU load. Your problem is IO. Reading from hard disk is very slow and should be performed in background when it's possible.
This...
Is an expensive call, each time it's called, a new
ImageIcon
class is created which is wrapping around theBufferedImage
data been read. UnlikeImageIcon
,ImageIO
will not buffer images and re-use them, instead, it will read the resource a new.This means, that each time the cell is rendered, the image is been fully reloaded.
Since, your loading the same image each time, simple load the image when you construct the class and make use of it when needed, for example...