0
votes

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?

2
Are you adding your jPanelImage to your JFrame?Jacob
Yes, I statically created it with NetBeansJames
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
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 resultsJames
Where is jPanelImage added to the content pane of a JFrame? Again, an minimal reproducible example would be very usefulcopeg

2 Answers

2
votes

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);

}
0
votes
jPanelImage.setVisibility(true);