1

I'm trying to get a jpeg to display on a JFrame and I'm noticing that some image files work while others do not. I'm using Eclipse and all of my images are in the project's root dir. When I run the Debugger and it hits my breakpoint it reports the imageheight and imagewidth as -1 on the images that will not display. It reports the correct values on the images that do display.

at first I thought it had something to do with the image sizes but after playing with resolutions in mspaint I've realized that this is not the case.

Here is my code so far:

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.File;

public class icon {

    public static void main(String[] args) {
        constructVNCForm();
    }

public static void constructVNCForm() {
        //Vars for GUI and essential layout code
        final JFrame frame = new JFrame("This should be on top");
        frame.setSize(800, 640);
        Container content = frame.getContentPane();
        frame.setVisible(true);

        //image code here:
        ImageIcon image = new ImageIcon("trial4.jpg");
        //ABOVE WORKS

        JLabel imageLabel = new JLabel(image);
        FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
        //add the image to the frame
        content.add(imageLabel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        content.setLayout(flow);
    }
}

Have any of you had any issues with ImageIcons? all of my jpegs are various sizes under 300x300. I'm kind of new to Java so if you have any suggestions on my code please advise.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • What happens when you try to load them using `ImageIO`? It provides more useful feedback than `ImageIcon`. You might also upload one of the problem images to an image share site & link to it if it is small in bytes (<100Kb). – Andrew Thompson Dec 09 '12 at 14:03
  • 4
    You should place `frame.setVisible(true)` as your last statement in `constructVNCForm` BTW. – Reimeus Dec 09 '12 at 14:08
  • @Reimeus answer is correct. – vels4j Dec 09 '12 at 14:11
  • 2
    And see also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Dec 09 '12 at 14:12
  • *"if you have any suggestions"* 1) Set the layout before adding objects to the panel. 2) As mentioned elsewhere, call `setVisible(true)` last. 3) Make the `FlowLayout` a `GridBagLayout` and wrap it in a scroll pane as shown in [this answer](http://stackoverflow.com/questions/13512612/browse-for-image-file-and-display-it-using-java-swing/13512826#13512826). 4) See also most of the points in [this answer](http://stackoverflow.com/questions/13787873/adding-buttons-using-gridlayout/13787922#13787922). – Andrew Thompson Dec 09 '12 at 14:12
  • Any chance of seeing (a link to) one of those images that does not work, any time soon? – Andrew Thompson Dec 10 '12 at 03:14

1 Answers1

2

Tested with few various size of images, the following works fine. Usually setVisible(true) method should be called at the end.

public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("This should be on top");
                frame.setSize(800, 640);
                ImageIcon image = new ImageIcon("someImage.jpg");
                JLabel imageLabel = new JLabel(image);
                FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
                frame.getContentPane().add(imageLabel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(flow);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
vels4j
  • 11,208
  • 5
  • 38
  • 63