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?
views:
276answers:
5
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
2010-01-10 20:05:11
But the code is not going to compile on Windows if I add "import com.apple.eawt.*". Am I right?
Cenk Alti
2010-01-10 20:28:00
You would have to run the code with reflection for it to compile.
Jay Askren
2010-01-27 05:28:44
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
2010-01-10 20:26:16
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
2010-01-10 20:32:51
+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
2010-01-10 20:43:47
+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
2010-01-10 20:56:01