views:

276

answers:

5

I want my application to integrate Mac OS X better. I've read Mac OS X Integration for Java and I've learned I have to import com.apple.eawt.* and write some extra code. But, if I do this, my application is not going to work on Windows because of missing reference. I could do this with preprocessor command if I write in C but this is Java. How can I do this without separating the code file into 2 branches?

A: 

You could have the code look at the OS-related System properties and only run the code when you see Mac OS X.

Michael Borgwardt
But the code is not going to compile on Windows if I add "import com.apple.eawt.*". Am I right?
Cenk Alti
You would have to run the code with reflection for it to compile.
Jay Askren
+2  A: 
Dan Dyer
I know this but my code is not going to compile on Windows if I add an "import com.apple.eawt.*". Actually my problem is not exactly the integration, I also want my code to be compiled on Windows.
Cenk Alti
If you have the JAR file that contains the com.apple.* classes, you can compile on any platform. I don't know which JAR it is, but you'll probably find it on your Mac somewhere. You won't need the JAR at runtime if you do as Michael suggests and avoid using those classes on non-Apple platforms. Anyway, the articles I linked to show that you can achieve quite a bit without needing any Apple classes.
Dan Dyer
+6  A: 

Macify it.

Jay Askren
Thank you. That's what I looking for.
Cenk Alti
So accept the answer?
bmargulies
+1 nice library, especially the dock icon extras.
Samuel Sjöberg
+1  A: 

Here are a few thing you can add at the beginning of your "main" function that will have your "swing" application look more native on MacOSX (works with 10.6 too)

System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Your app name");
try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} 
catch (ClassNotFoundException ex) {}
catch (InstantiationException ex) {}
catch (IllegalAccessException ex) {}
catch (UnsupportedLookAndFeelException ex) {}

this will have your menu bar go in the mac menu bar. It will set your app name in the menu bar and will set your app look and feel to mac OSX l&f.

Its not perfect but it's a quick start :)

Snowangelic
+3  A: 

I used reflection to see if the com.apple.whatever class was there, and if so, I invoked it.

Worked very well, and does not create breaking imports.

Thorbjørn Ravn Andersen