Cenk Alti
Cenk Alti

Reputation: 2888

Java-Mac OS X integration without breaking platform independence

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?

Upvotes: 5

Views: 1470

Answers (6)

DinoTheDinosore
DinoTheDinosore

Reputation: 21

I found this helpful, and want to return the favor by updating. The "Mac OS X Integration for Java" web page has a link called "J2SE 5.0 Apple Extensions Reference" to the com.apple.eawt documentation that (as of Dec 2015) no longer works - says "Sorry, that page cannot be found". I tried searching the Apple Developer site for it without success. The good news is I found this link that has the source code for com.apple.eawt - http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/macosx/classes/com/apple/eawt

Using com.apple.eawt, I found that the method removeAboutMenuItem() is deprecated and says to "use setAboutHandler(AboutHandler) with a null parameter" instead. But setAboutHandler(null) doesn't work, and the deprecated method does.

Upvotes: 0

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.

Upvotes: 3

almathie
almathie

Reputation: 731

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 :)

Upvotes: 1

Jay Askren
Jay Askren

Reputation: 10444

Macify it.

Upvotes: 9

Dan Dyer
Dan Dyer

Reputation: 54465

I've previously bookmarked these pages which have good advice about tweaking Swing applications for OS X.

Upvotes: 2

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

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

Upvotes: 0

Related Questions