简体   繁体   中英

Java icon won't show twice

I'm making a small Java application that have to show some images in a JLabel named picLabel.

I have a JList of Photo objects (that contains an InputStream of an image, read from a database).

Here is the code of the JList ValueChanged event listener:

private void photoListValueChanged(javax.swing.event.ListSelectionEvent evt) {     
    if (evt.getValueIsAdjusting() == false && photoList.getSelectedIndex() != -1) {
        photo = (Photo) photoList.getSelectedValue();
        BufferedImage image = ImageIO.read(photo.getContent()) ;
        if(image != null) {
            picLabel.setIcon(new ImageIcon(image));
        }
}

It works perfectly for the first time I select each element from the list. But if I choose again an element that was already selected (and the image was already shown), it simply don't show the image, leaving the JLabel as it was before.

Am I missing something?

Once you have read the image once from the input stream, the stream is at its end, and reading a second time won't read anything. The Photo class should read from the stream and store everything read as a byte array, or as a BufferedImage or ImageIcon directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM