问题
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;
}
}
回答1:
This...
jLabel.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("blank.png"))));
Is an expensive call, each time it's called, a new ImageIcon
class is created which is wrapping around the BufferedImage
data been read. Unlike ImageIcon
, 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...
public class DefaultTableCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
private Icon icon;
public DefaultTableCellRenderer() throws IOException {
icon = new ImageIcon(ImageIO.read(getClass().getResource("blank.png");
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean selected, boolean focus, int row, int col) {
super.getTableCellRendererComponent(table, value, selected, focus, row, col);
setIcon(icon);
if (row == 1) {
setText("Image Data");
setBackground(Color.LIGHT_GRAY); // You may need to set the opaque state for this to work...
} else {
setText("Final");
}
return this;
}
}
回答2:
Some suggestions for you:
- You can load the images at the start of application using multithreading. Use the class Executors to do it. Important is: you must load all the images before you show your UI.
- Another possibility is asynchronious loading of images. Use SwingWorker to load images. The loading must be implemented in the method
doInBackground()
. When some images are loaded you can use the methodspublish()
/process()
to update your table model with new images. - You can combine both and use
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.
来源:https://stackoverflow.com/questions/25659273/jtable-image-render-is-taking-too-much-load-in-application