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

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
Hello everyone at MacRumors Forums.

I have been having trouble with a little Java project I am working on.
I have been using a Java learning environment called BlueJ to write small program, and have switched to XCode to make it a full Mac app.
My trouble is, I had an image displaying in a JFrame in BlueJ, but I can't get it working in XCode.
In my buildInterface() method (which is called in the constructor) I have this code:

ImageIcon icon = createImageIcon("money.gif", "The app title icon.");
top = new JLabel(icon);
header.add(top);

My createImageIcon method looks like this:

protected static ImageIcon createImageIcon(String path, String description) {
java.net.URL imgURL = Asset_Monitor.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(path, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}

The "money.gif" image file is located in the root directory of the project.
As I said it displays fine in BlueJ but I can't get it working in XCode.
Any help would be greatly appreciated.
 
Do you get the "Couldn't find file: someFile" error or does it just not show?
 
It does display the error in the log, yes.
I've done a "get info" on money.gif, and it says its path is just money.gif, with it set to 'Relative to Enclosing Group'. 'Relative to Build Product' says the path is ../money.gif.
 
System.out.println(System.getProperty("java.class.path"));

What is the runtime class path?
 
System.out.println(System.getProperty("java.class.path"));

=

/Users/stephenanthony/Documents/Money app/XCode/Asset Monitor/build/Asset Monitor.app/Contents/Resources/Java/Asset Monitor.jar
 
What's the contents of the jar file?

In terminal:

jar tf "/Users/stephenanthony/Documents/Money app/XCode/Asset Monitor/build/Asset Monitor.app/Contents/Resources/Java/Asset Monitor.jar"
 
META-INF/
META-INF/MANIFEST.MF
AboutBox$SymWindow.class
AboutBox.class
Asset_Monitor$1.class
Asset_Monitor$10.class
Asset_Monitor$2.class
Asset_Monitor$3.class
Asset_Monitor$4.class
Asset_Monitor$5.class
Asset_Monitor$6.class
Asset_Monitor$7.class
Asset_Monitor$8.class
Asset_Monitor$9.class
Asset_Monitor$clearActionClass.class
Asset_Monitor$closeActionClass.class
Asset_Monitor$copyActionClass.class
Asset_Monitor$cutActionClass.class
Asset_Monitor$newActionClass.class
Asset_Monitor$openActionClass.class
Asset_Monitor$pasteActionClass.class
Asset_Monitor$saveActionClass.class
Asset_Monitor$saveAsActionClass.class
Asset_Monitor$selectAllActionClass.class
Asset_Monitor$undoActionClass.class
Asset_Monitor.class
Asset_Monitorstrings.properties
PrefPane$1.class
PrefPane.class

The .jar file is located in the application package in a folder called Java. The money.gif image is one folder back, in Resources, along with the application .icns file.
 
How about this? Or some variant of this.
Code:
protected static ImageIcon createImageIcon(String path, String description) {
  java.net.URL imgURL = Asset_Monitor.class.getResource(path);
  if (imgURL != null) {
    return new ImageIcon(path, description);
  } else {
    imgURL = Asset_Monitor.class.getResource("../"+path);
    if (imgURL != null) {
      return new ImageIcon("../"+path);
    } else {
      System.err.println("Couldn't find file: " + path);
      return null;
    }
  }
}
Or in the worst case:
java.io.File classPath = new File(System.getResource("java.class.path"));
classPath = classPath.getParentFile().getParentFile();
imgURL = Asset_Monitor.class.getResource(classPath.getPath()+path);
 
The first suggestion didn't work. For the second suggestion I put this:

protected static ImageIcon createImageIcon(String path, String description) {
java.io.File classPath = new File(System.getResource("java.class.path"));
classPath = classPath.getParentFile().getParentFile();
URL imgURL = Asset_Monitor.class.getResource(classPath.getPath()+path);
}

I get a build error on the java.io.File line. I have imported java.io.File. Cannot resolve symbol : method getResource
 
stadidas said:
The first suggestion didn't work. For the second suggestion I put this:

protected static ImageIcon createImageIcon(String path, String description) {
java.io.File classPath = new File(System.getResource("java.class.path"));
classPath = classPath.getParentFile().getParentFile();
URL imgURL = Asset_Monitor.class.getResource(classPath.getPath()+path);
}

I get a build error on the java.io.File line. I have imported java.io.File. Cannot resolve symbol : method getResource
Sorry, System.getProperty(...) and also java.io.File at both places if you want to be consistent.
 
stadidas said:
ok thanks. What should the return statement be? Sorry, I've never seen that sort of code before, I'm not really sure what it's doing.
My intention was that you combined it with the original code. Basically it just gets the classpath "/Users/stephenanthony/Documents/Money app/XCode/Asset Monitor/build/Asset Monitor.app/Contents/Resources/Java/Asset Monitor.jar", chops off "Java/Asset Monitor.jar" and then uses that to create the direct path to the image file.
Code:
protected static ImageIcon createImageIcon(String path, String description) {
  java.net.URL imgURL = Asset_Monitor.class.getResource(path);
  if (imgURL != null) {
    return new ImageIcon(path, description);
  } else {
    java.io.File classPath = new java.io.File(System.getProperty("java.class.path"));
    classPath = classPath.getParentFile().getParentFile();
    System.out.println("Xcode loading resource: "+classPath.getPath()+path);
    imgURL = Asset_Monitor.class.getResource(classPath.getPath()+path);
    if (imgURL != null) {
      return new ImageIcon(imgURL, description);
    } else {
      System.err.println("Couldn't find file: " + path);
      return null;
    }
  }
}

but try this first:

Code:
protected static ImageIcon createImageIcon(String path, String description) {
  java.net.URL imgURL = Asset_Monitor.class.getResource(path);
  if (imgURL != null) {
    return new ImageIcon(path, description);
  } else {
    imgURL = Asset_Monitor.class.getResource("../../"+path);
    if (imgURL != null) {
      return new ImageIcon("../../"+path, description);
    } else {
      System.err.println("Couldn't find file: " + path);
      return null;
    }
  }
}

Edit: Finished removing obvious errors, but no guarantees.
 
The second thing you put didn't work.

The first method couldn't find it but gave this:
Xcode loading resource: /Users/stephenanthony/Documents/Money app/XCode/Asset Monitor/build/Asset Monitor.app/Contents/Resourcesmoney.gif

There's a slash missing between 'Resource' and ,money', but I don't know how to put it in.

Thanks for helping me, I've not being doing Java for long.
 
stadidas said:
There's a slash missing between 'Resource' and ,money', but I don't know how to put it in.

Code:
    System.out.println("Xcode loading resource: "+classPath.getPath()+"/"+path);
    imgURL = Asset_Monitor.class.getResource(classPath.getPath()+"/"+path);

stadidas said:
Thanks for helping me, I've not being doing Java for long.
No problem.

I'm glad to help, but hopefully someone else will read this thread and have a much more elegant solution.
 
stadidas said:
Still won't find it, even though it's the correct path... I don't know what to do!
Strange. My code had an error in it for a while. Are you using the latest edit with:
return new ImageIcon(imgURL, description);
in it?

Also, does it still print the "Couldn't find file: someFile" error?
 
I am using the new version, and it does print the error.
Sorry I've taken a while to reply, I'm also trying to bug fix CSS for IE... it's been a hard day.

Also one other problem I have is that the JLabels which are set like this:

label4 = new JLabel(" Money to spend per day: £" + perDay );
labels.add(label4);

look fine in BlueJ, but look like this in XCode:

labelerror.png


I don't know what's causing that either.
 
Code:
protected static ImageIcon createImageIcon(String path, String description) {
  java.net.URL imgURL = Asset_Monitor.class.getResource(path);
  if (imgURL != null) {
    return new ImageIcon(path, description);
  } else {
    java.io.File imgFile = new java.io.File(System.getProperty("java.class.path"));
    imgFile = imgFile.getParentFile().getParentFile();
    imgFile = new java.io.File(imgFile, path);
    System.out.println("Xcode loading resource: "+imgFile.getPath());
    if (imgFile.exists()) {
      return new ImageIcon(imgFile.getPath(), description);
    } else {
      System.err.println("Couldn't find file: " + path);
      return null;
    }
  }
}
How about this then?
 
ok, now I have another problem.
Not so big this time - I deleted the Asset_Manager.icns file, and have replaced it with another one of the same place, in the same location. The trouble is XCode is building the application with the default application icon, not the icon I have supplied. How can I get it to use the right icon?
 
I'm not entirely sure. How to set the app icon has always confused me a little bit. You could try to clean the project and then build it again, or you could try to give it another name and then change it to the new name in the settings and then clean/build.

If all else fails, you can change the icon for the finished product using the Get Info in Finder and when you distribute the app in a .dmg, the icon will go with it.
 
stadidas said:
What are you referring to by 'it'?
Oh sorry ...

You could try to clean the project and then build it again, or you could try to give the icon file another name and then change the project settings to use the new name and then clean/build.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.