I am just getting started with XCode/Java and wanted to create a simple app that displays an image. I created a new "Java Swing Application" project called SwingTest and added the following code to the SwingTest constructor just before the setSize and setVisible calls. Nothing displays and I am trying to figure out why. I added the image to the project by dragging and dropping it into the Resources directory of the project and I can see it inside the contents of the built application package at /Contents/Resources/sample.jpg but it won't display. Any ideas are welcome, thanks.
Attempt #1:
ImageIcon pIcon = new ImageIcon("sample.jpg");
JLabel imgLabel = new JLabel(pIcon);
this.add(imgLabel);
Attempt #2:
String imgName = "sample.jpg";
java.net.URL imgURL = getClass().getResource(imgName);
Toolkit tk = Toolkit.getDefaultToolkit();
MediaTracker m = new MediaTracker(this);
Image img = tk.getImage(imgURL);
ImageIcon i = new ImageIcon(img);
JLabel imgLabel = new JLabel(i);
Attempt #1:
ImageIcon pIcon = new ImageIcon("sample.jpg");
JLabel imgLabel = new JLabel(pIcon);
this.add(imgLabel);
Attempt #2:
String imgName = "sample.jpg";
java.net.URL imgURL = getClass().getResource(imgName);
Toolkit tk = Toolkit.getDefaultToolkit();
MediaTracker m = new MediaTracker(this);
Image img = tk.getImage(imgURL);
ImageIcon i = new ImageIcon(img);
JLabel imgLabel = new JLabel(i);