1

This is the simplest code to me.. yet failing to show up my uploaded picture from my desktop, Rather when I resize the JFrame window with mouse, it shows up.

This case happens both in Netbeans and Eclipse... can you explain the reason behind this???? Thanks in advance..

    JFrame f=new JFrame("My Frame");
        f.setSize(1500,1500);
        f.setVisible(true);
    //  f.setLayout(new FlowLayout());
        JLabel l=new JLabel("A pic in next Label");
        f.add(l);
        
        
        JLabel lp=new JLabel();
        lp.setIcon(new ImageIcon("aaa.jpg"));
        f.add(lp);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 3
    You are showing the frame before adding the labels. So only when you resize the frame it re-renders and you see the image. You should fist add the labels and then call .pack() and then .visible(). Hope it helps – MissingSemiColon Jul 15 '21 at 15:15
  • 1
    You have to setSize and setVisible after you've added the JLabel components. – Gilbert Le Blanc Jul 15 '21 at 15:16
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Jul 15 '21 at 15:37
  • As an aside. it's odd that this `// f.setLayout(new FlowLayout());` is commented out & the code is trying to add 2 labels (without constraints specified) to a `BorderLayout` (the default layout of the content pane of a `JFrame`). Only one will show, but setting a `FlowLayout` would be a way around that. It was .. pure luck that in my example, I reduced it from two components to a single one - which will also work. – Andrew Thompson Jul 15 '21 at 19:19

1 Answers1

4

This is an MRE / SSCCE that demonstrates how to load an image and display it in a correctly sized frame. Read the code comments for further explanations.

enter image description here

Note, the code might use an EmptyBorder for white space around the label. But I wanted to demonstrate it is not a pixel bigger (or smaller) than needed.

import javax.swing.*;
import java.net.URL;

public class DisplayAndSizeAnImageFrame {

    // pathetic exception handling used here, BNI
    DisplayAndSizeAnImageFrame() throws Exception {
        JFrame f = new JFrame("Image Frame");
        //f.setSize(1500,1500); Just a guess. Use pack() for right size
        //f.setVisible(true);   not YET
        JLabel l=new JLabel("A pic in this label");
        f.add(l);
        
        // JLabel lp=new JLabel(); Don't need to use TWO labels!
        l.setIcon(new ImageIcon(new URL("https://i.stack.imgur.com/P59NF.png")));
        // Image from http://stackoverflow.com/q/19209650/418556
        
        // now all components are added, size the frame..
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                new DisplayAndSizeAnImageFrame();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433