开发者

Reload the images in same Label(without creating any new)

I have a code that will show the image got from a local client. It gets different images in different time. Hence , i want to show all the images one by one in the same label refreshing each time. The code below will generate new labe开发者_开发技巧l each time the object is received. How can i modify so that i will get the output as i want?

// For each connection it will generate a new label.   

public void received(Connection connection, Object object) {
    if (object instanceof Picture) {

        Picture request = (Picture) object;
        try {
            System.out.println(request.buff.length);
            InputStream in = new ByteArrayInputStream(request.buff);
            BufferedImage image = ImageIO.read(in);
            JFrame frame = new JFrame("caption");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Dimension dimension = new Dimension(image.getWidth(), image.getHeight());

            JLabel label = new JLabel(new ImageIcon(image)); //displays image got from each connection
            JScrollPane pane = new JScrollPane(label, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
            frame.getContentPane().add(pane);
            frame.setSize(dimension);
            frame.setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex);
        }
    }
}


I guess you can use the same JLabel and call the setIcon method on the same instance. You should also reuse the same JFrame and JScrollPane. So, you should initialize them in a separate method and call just the setIcon method when you receive a new object.


The code will not only generate new JLabel each time, but also new JFrame, new JScrollPane etc...

Separate the code into two methods init and receive. init will be executed only at the beginning and will create all the "around" while receive will update the image.

Basic example:

JFrame frame;
JLabel label;
JScrollPane pane;
// ...  
public void init() {
    frame = new JFrame("caption");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Dimension dimension = new Dimension(someDefaultHeight, someDefaultWidth);
    label = new JLabel(); //displays image got from each connection
    JScrollPane pane = new JScrollPane(label, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    frame.getContentPane().add(pane);
    frame.setSize(dimension);
    frame.setVisible(true);
}


public void received(Connection connection, Object object) {
    if (object instanceof Picture) {
        Picture request = (Picture) object;
        try {
            System.out.println(request.buff.length);
            InputStream in = new ByteArrayInputStream(request.buff);
            BufferedImage image = ImageIO.read(in);
            Dimension dimension = new Dimension(image.getWidth(), image.getHeight());
            label.removeAll();
            label.setIcon(new ImageIcon(image));
            frame.setSize(dimension);
            label.revalidate();
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println(ex);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜