5

I'm writing a Java application designed for all platforms, but specifically, I am working on OS X appearance and integration (I use NetBeans on my MacBook Pro). I'd like to access the application's menu, i.e. the bolded menu named after the application. I want to register listeners for the About and Quit items, as well as show the Preferences item. I wouldn't mind adding a few more items of my own, too.

So, how do I do this? I have seen previous posts refer to OSXAdapter, but the geniuses at Apple decided to remove it from their library (or rename it ambiguously) because all links redirect to the main page, and all my searching has been fruitless. I've also seen a MacOSAppAdapter class, but I am unsure how to use it. All the importing and new classes and hierarchies is bit confusing.

EDIT:

This is what I did to tie into the About, Preferences, and Quit items:

I made a new class, MacOSXAboutHandler, that extends

com.apple.eawt.Application

Its constructor simply invokes

setAboutHandler(AboutHandler aH)

and I provide it with my own listener that extends

AboutHandler

In my main class I determine if I am running on a Mac using

System.getProperty("os.name").contains("mac")

If this is true, then I simply create a new instance of MacOSXAboutHandler. The constructor adds my handler, and whenever the application is run (or even tested within NetBeans), clicking the About... item on the application's bolded menu executes the code I specified in my AboutHandler.

The same is done for preferences and quit, simply replacing occurrences of "about" with the appropriate action. All these handlers are written as any other Java listener would be.

Machavity
  • 30,841
  • 27
  • 92
  • 100
CAG Gonzo
  • 589
  • 1
  • 5
  • 13

1 Answers1

3

You might want to have a look at Bringing your Java Application to Mac OS X and (more importantly) Bringing your Java Application to Mac OS X Part 2 and Bringing your Java Application to Mac OS X Part 3

You might Java System Property Reference for Mac of use

You may want to take a look at Apple's Java 6 Extensions API, from my brief reading, it would appear that you basically want to use the default instance if com.apple.eawt.Application and supply the handlers you need (such as setAboutHandler).

You may also want to read through The Java on Mac OS X About, Quit and Preferences menu items and events article, which should provide some more additional hints.

Jouni Aro
  • 2,099
  • 14
  • 30
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I have read parts of those. I tried conceptualizing their process and code and it left me confused. I misunderstood their set-up. Here's what I did to tie into the About menu item (which works for Preferences, Quit, etc.): Set up a custom class (say CustomMac) that extends com.apple.eawt.Application. Within that class (or elsewhere) make a listener class that extends AboutHandler and do what you need to do there. The CustomMac constructor simply adds a new instance of your listener using `setAboutHandler(AboutHandler aH)`. – CAG Gonzo Aug 01 '12 at 04:08
  • I'm not in front of my Mac at the moment, so I'll have a look when I get home (I'm interested in the answer in the long term any way ;)) – MadProgrammer Aug 01 '12 at 04:10
  • @StephaneGrenier Which one? There's 6 there and they all open for me. – MadProgrammer Mar 24 '13 at 04:51
  • @StephaneGrenier They do now – MadProgrammer Mar 24 '13 at 04:52