0

So I'm trying to display an image, in a jPanel, within a jFrame.

(I suppose it's that way you display a JPEG/PNG in a jFrame) Here's what I'm doing:

In the constructor of the jFrame, I download the image, create an ImageIcon, and a jLabel dynamically (set the icon), and insert it in a jPanel.

I have previously created the jPanel with NetBeans IDE, on which jPanelImage is defined in the initComponents().

If I go through the code with the debugger, he downloads the image propery without throwing any exceptions.

It also runs the code properly without any problem.

But my jFrame continues empty. Here's the code:

    public TransmissionFrame() {
        initComponents();

        init();
    }

    private void init() {

    JLabel label = new JLabel("Java Technology Dive Log");
    ImageIcon image = null;
    try {
        image = new ImageIcon(ImageIO.read(new URL("http://i.imgur.com/6mbHZRU.png")));
    } catch(MalformedURLException mue) {
        mue.printStackTrace();
    } catch(IOException ioe) {
        ioe.printStackTrace();
    } 
    label.setIcon(image);
    jPanelImage.add(label);
    jPanelImage.validate();
}

But my jFrame and the jPanel are still empty, why?

James
  • 3,580
  • 3
  • 25
  • 36
  • Are you adding your jPanelImage to your JFrame? – Jacob Sep 02 '16 at 18:38
  • 1
    Yes, I statically created it with NetBeans – James Sep 02 '16 at 18:40
  • You'll want to move your initialization of label and addition of the label into the try catch block. I would also move the JLabel declaration itself in there and try JLabel label = new JLabel(image); Then add label to the panel. – Jacob Sep 02 '16 at 18:44
  • I go through the try block with the debugger, and I don't get any exceptions, but I tried what you said, and I get the same results – James Sep 02 '16 at 18:47
  • Please post an [mcve]...it is tough to pin down what the exact problem is without guessing – copeg Sep 02 '16 at 18:50
  • I've edited the post, and clearified some situations. Sorry, please help me out! Thanks – James Sep 02 '16 at 18:55
  • 1
    Where is `jPanelImage` added to the content pane of a JFrame? Again, an [mcve] would be very useful – copeg Sep 02 '16 at 18:56
  • [Here's](https://gist.github.com/anonymous/c4750c359e5145dbc106b6dff0e70d77) a working example of working with JLabels as images. – Jacob Sep 02 '16 at 18:58
  • Thank you @Jacob for the example, but I tried it and I get exactly the same result. An empty jFrame again – James Sep 02 '16 at 19:02
  • Did you try just that or did you adapt that to what you had before? – Jacob Sep 02 '16 at 19:04
  • I adapted, but my frame hadn't basically any code ... – James Sep 02 '16 at 19:06
  • I removed the jPanelImage – James Sep 02 '16 at 19:06
  • I think your panel isn't being added to your JFrame. Make sure your JFrame is calling getContentPane().add(panel). You can also remove the panel.validate(), as you won't need that. – Jacob Sep 02 '16 at 19:08
  • I don't know if it's relevant or not, but I'm starting the frame from a process like this – James Sep 02 '16 at 19:12
  • TransmissionFrame frame = new TransmissionFrame(); frame.setVisible(true); – James Sep 02 '16 at 19:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122544/discussion-between-jacob-and-firecat). – Jacob Sep 02 '16 at 19:14

2 Answers2

2

update, assigning a layout to your JPanel could be the solution

 public TransmissionFrame() {
        initComponents();

        init();
    }

 private void init() {
JLabel label = new JLabel("Java Technology Dive Log");
ImageIcon image = null;
try {
    image = new ImageIcon(ImageIO.read(new URL("http://i.imgur.com/6mbHZRU.png")));
} catch(MalformedURLException mue) {
    mue.printStackTrace();
} catch(IOException ioe) {
    ioe.printStackTrace();
} 
label.setIcon(image);
jPanelImage.setLayout(new FlowLayout()); 
jPanelImage.add(label);

}
Dev. Joel
  • 1,127
  • 1
  • 9
  • 14
  • `size of the panel ` setSize won't do anything if the the Container uses a LayoutManager, and a LayoutManager should size the JLabel containing `Icon` appropriately. See http://stackoverflow.com/questions/1783793/java-difference-between-the-setpreferredsize-and-setsize-methods-in-compone – copeg Sep 02 '16 at 19:27
0
jPanelImage.setVisibility(true);