0

I'm trying to add the image "Pic.png" to this JLabel "label1" and display it on the JPanel "panel1" on a JFrame "window1". But it when I hit run it doesn't display my image. Anyone help? (I read about adding it to the source file or something but I'm not really sure what I'm doing because I'm new to Java. Will it not be able to access the picture without the image being in the source?)

public class UIForIshidaQuery {

    public static void main(String[] args) {
        System.out.println("Running...");

        JFrame window1 = new JFrame();
        window1.setVisible(true);
        window1.setSize(1080, 720);
        window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel1 = (JPanel) window1.getContentPane();
        JLabel label1 = new JLabel();
        panel1.setLayout(null);
        ImageIcon image = new ImageIcon("C:\\Users\\BC03\\Pictures\\Saved Pictures\\Other\\Pic.png");
        label1.setIcon(image);
        label1.setBounds(500, 500, 500, 500);
        panel1.add(label1);
    }
}
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
SuperHanz98
  • 2,090
  • 2
  • 16
  • 33
  • Does `new File("C:\\Users\\BC03\\Pictures\\Saved Pictures\\Other\\Pic.png").exists()` return true ? – Arnaud Aug 25 '16 at 09:22
  • See the examples cited [here](http://stackoverflow.com/tags/embedded-resource/info) in the [tag:embedded-resource] tag. – trashgod Aug 25 '16 at 09:25
  • Yes it returns true@Berger – SuperHanz98 Aug 25 '16 at 09:27
  • @Cutter : you may use the `ImageIcon`'s constructor taking an URL, and retrieve the URL from your File's path, see : http://stackoverflow.com/questions/6098472/pass-a-local-file-in-to-url-in-java – Arnaud Aug 25 '16 at 09:39
  • @Berger If it's the right path, converting it to an URL will make no difference. In fact, it will also make no difference if it is the wrong path. – Andrew Thompson Aug 25 '16 at 09:47
  • @Andrew Thompson : You're right, I misunderstood how the constructor with the String worked. – Arnaud Aug 25 '16 at 09:50

2 Answers2

2

The window should be set visible as the last call. Don't use null layouts1. This works.

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

public class UIForIshidaQuery {

    public static String url = "https://i.stack.imgur.com/gJmeJ.png";

    public static void main(String[] args) throws MalformedURLException {
        System.out.println("Running...");

        JFrame window1 = new JFrame();
        window1.setSize(1080, 720);
        window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel1 = (JPanel) window1.getContentPane();
        JLabel label1 = new JLabel();
        //panel1.setLayout(null);
        ImageIcon image = new ImageIcon(new URL(url));
        label1.setIcon(image);
        //label1.setBounds(500, 500, 500, 500);
        panel1.add(label1);
        window1.setVisible(true);
    }
}
  1. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • The above information is correct, pixel perfect layout is difficult to achieve in Java Swing. So for the sake of just checking your image in the existing code you can run and try to maximize/minimize your window and you will see your image appearing which is not going to appear instant when you run the code. – Haider Raza Aug 25 '16 at 09:51
1

If you are using IntelliJ IDEA:

  1. Right click your project root directory and select New > Directory;
  2. Call the new directory 'resources' for example;
  3. Right click the newly made directory and select Mark Directory As > Resources Root;
  4. Add your image file in the directory;
  5. Access your file properly: CurrentClass.class.getClassLoader().getResource("pic.png").getFile();

The ImageIcon could be initialized like this:

File file = new File(CurrentClass.class.getClassLoader().getResource("pic.png").getFile());
        BufferedImage image = null;
        try {
            image = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ImageIcon imageIcon = new ImageIcon(image);
Thibstars
  • 1,085
  • 1
  • 9
  • 30