0

I'm working on a program that will read a folder of images into a JList, and the picture that is selected in the JList will be drawn into a JPanel. I have two classes: ImageViewerPanel, which creates the panel to display the selected picture. I then have ImageViewerUI which will draw the JList, and the ImageViewerPanel will be added into the ImageViewerUI class. Here is the relevant code from the ImageViewerPanel class.

public ImageViewerPanel() {
    initComponents();
}

public void setImage(BufferedImage image) {
    this.image = image;
    repaint();
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (scaled == false) {
        g.drawImage(image, 0, 0, null);
    }else if(scaled == true) {
        g.drawImage(image, 0, 0, 80, 80, null);
    }

Here is the code from the ImageViewerUI class that is to refresh the panel with the image:

    ImageViewerPanel imagePanel = new ImageViewerPanel();
    BufferedImage displayedImage;
    BufferedImage originalImage;

public ImageViewerUI() {
    initComponents();
    loadListWithImageFilenames();
    updateImagePanel();
    updateThumbnailImagePanel();
}

public final void updateImagePanel() {
    try {
        String currFile = (String) ("Images/" + imageList.getSelectedValue());
        displayedImage = ImageIO.read(new File(currFile));
        imagePanel.setImage(displayedImage);
    } catch (IOException ex) {
        Logger.getLogger(ImageViewerUI.class.getName()).log(Level.SEVERE, null, ex);
    }

public final void updateThumbnailImagePanel() {
    try {
        String currFile = (String) ("Images/" + imageList.getSelectedValue());
        originalImage = ImageIO.read(new File(currFile));
        imagePanel.setScaled(true);
        imagePanel.setImage(originalImage);
        imageViewerPanel1.add(imagePanel);
        imagePanel.repaint();
    } catch (IOException ex) {
        Logger.getLogger(ImageViewerUI.class.getName()).log(Level.SEVERE, null, ex);
    }

}

The issue I am having is that the image is not being displayed in the panel. Anyone know why?

  • for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, [create an BufferedImage on the fly](http://stackoverflow.com/a/7944388/714968) – mKorbel Apr 02 '13 at 15:46
  • We need a small complete runnable example code to help you. For one thing, this doesn't seem to make any sense: `imageViewerPanel1.add(imagePanel);`. Why would you add the image panel to the layout every time the thumbnail is impdated? – Gene Apr 02 '13 at 15:47
  • If only someone could come up with a way to make Swing code short, self contained, and runnable. Runnable code can be debugged by running it. – Gilbert Le Blanc Apr 02 '13 at 15:47
  • @mKorbel I've looked at multiple questions on this topic, but none of them help me. –  Apr 02 '13 at 15:50
  • @Gene I was just trying everything I could to get it to display the picture. Also, I added my constructors if that helps. –  Apr 02 '13 at 15:51
  • @DarthCthulhu not true at all, somewhere must be described something about your issue, 1. override getPreferredSize for container where paintComponent is applyed, 2. then use getWeight/Height instead of getScaledInstance, 3. scalledInstance is simple asynchronous, nothing can be displayed in real time (read API about), nothing cleaver without and SSCCE. – mKorbel Apr 02 '13 at 15:55
  • @mKorbel Well I've actually verified that the issue is with the `ImageViewerPanel` being called into the `ImageViewerUI` class. I went into the Panel class and directly read an image into the constructor there and it worked. But I need to read them in from the UI class so that the picture will change when a different one is selected on the JList. –  Apr 02 '13 at 16:01

1 Answers1

1
imageViewerPanel1.add(imagePanel);
imagePanel.repaint();

When you add components to a visible GUI the code should be:

imageViewerPanel1.add(imagePanel);
//imagePanel.repaint();
imageViewerPanel1.revalidate();
imageViewerPanel1.repaint();

All components are created with a size of (0, 0) so there is nothing to paint. The revalidate() invokes the layout manager which in turn gives a size to the compoenent.

As mentioned above you will also need to override the getPreferredSize() method of you imagePanel so the layout manager can determine the proper size of the panel.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • @DarthCthulhu then to use `CardLayout` instead of `container.revalidate()` and `container.repaint()` – mKorbel Apr 02 '13 at 16:16