Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chrisS

macrumors member
Original poster
Sep 21, 2007
31
0
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);
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
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);
Your code looks just fine. The problem is that your application's working directory isn't set to be inside the application package - hence why the application can't find the image and thus displays nothing. I believe this is an option somewhere in the project build settings.

DISCLAIMER: I am new to XCode too. I normally use Eclipse for Java development because, when I try to use XCode I get warnings about some imported classes being deprecated, and I can't, for the life of me, figure out why. :eek:
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Are you adding these elements to a JPanel or JFrame somewhere else that you didn't show?

Todd

(EDIT - I didn't go through the hoop of creating the same test app, and doing that might have answered this question.)
 

chrisS

macrumors member
Original poster
Sep 21, 2007
31
0
I am adding to main class which extends JFrame. I tried changing the Edit Active Executable under the Project menu, specifically the "Set the working directory to:", but none of the choices make a difference.
 

chrisS

macrumors member
Original poster
Sep 21, 2007
31
0
If I add a "System.out.println(System.getProperty("java.class.path"));", I get two directories ...

/Users/chris/SwingTest/build/Debug/SwingTest.app/Contents/Resources/Java/SwingTest.jar:
/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/.compatibility/14compatibility.jar

How do I use just the first directory ?
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
I am adding to main class which extends JFrame. I tried changing the Edit Active Executable under the Project menu, specifically the "Set the working directory to:", but none of the choices make a difference.
Is your JFrame showing up on screen, and you're just not getting any image to appear inside it? If that's the case, try using an absolute path (starting with a /, such as /Applications/YourApp.app/Contents/Resources/image.jpg ) and see if that gets the image to load. If it does, that confirms that the problem is a working directory issue. If it still doesn't load, there is some other problem - one possible cause is your JLabel call. I usually make JLabels that contain images the following way:
Code:
JLabel blah = new JLabel("",ii)
where ii is an ImageIcon. The "" makes the label have no text.
 

chrisS

macrumors member
Original poster
Sep 21, 2007
31
0
Ok, I copied the image to the desktop and changed code to ...

ImageIcon ii = new ImageIcon("/Users/chris/Desktop/sample.jpg");
JLabel imgLabel = new JLabel(ii);
imgLabel.setVisible(true);
this.add(imgLabel);

The window still appears, but still has the default striped background, nothing else.
 

chrisS

macrumors member
Original poster
Sep 21, 2007
31
0
Found the answer...

I changed ...

this.getContentPane().setLayout(null);

to ...

this.getContentPane().setLayout(new GridLayout(0, 1));

and it works. The top line was generated for my Xcode project by Xcode, weird.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.