JTable Image render is Taking too much load In Application

最后都变了- 提交于 2019-12-13 00:49:47

问题


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:

  1. 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.
  2. 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 methods publish()/process() to update your table model with new images.
  3. You can combine both and use Executors in doInBackground() 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!