> In an attempt to handle the special CMD+Q on a Mac that resides in the
> application menu I had to put in the following code:
[quoted text clipped - 19 lines]
> way I can use this code and still maintain the cross-platform aspect of
> java?
Sure; reflection can do what you want. The naive way would look like
this (omitting exception handling).
if (System.getProperty("os.name").toLowerCase()
.indexOf("mac") != -1)
{
Class appc = Class.forName("com.apple.eawt.Application");
Object app = appc.newInstance();
Class lc = Class.forName(
"com.apple.eawt.ApplicationListener");
Object listener = Proxy.newProxyInstance(
getClassLoader(), new Class[] { lc },
new InvocationHandler() {
public Object invoke(
Object proxy,
Method method,
Object[] args)
{
if (method.getName().equals("handleQuit")
{
attemptToQuit();
}
}
});
Method m = appc.getMethod("addApplicationListener", lc);
m.invoke(app, listener);
}
Of course, that's the naive way. A better way would be to put all that
code into a class that implements some interface, and then do something
like this:
if (System.getProperty("os.name").toLowerCase()
.indexOf("mac") != -1)
{
AppCloseHandler h = Class.forName(
"mypackage.MacintoshCloseHandler").newInstance();
h.setupClose();
}
AppCloseHandler is an interface that you write. Inside of the class
mypackage.MacintoshCloseHandler, you can make static use of the Mac-
specific classes, since this code will only get loaded when those
classes are available.
I still find it hard to believe that WindowListener.windowClosing
doesn't work properly on the Mac... but if you've tried it, I guess I
can't argue since I don't have a Mac to try it with.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Steve W. Jackson - 31 Mar 2006 19:47 GMT
[ snip ]
> I still find it hard to believe that WindowListener.windowClosing
> doesn't work properly on the Mac... but if you've tried it, I guess I
> can't argue since I don't have a Mac to try it with.
WindowListener.windowClosing works exactly the same way on a Mac as it
does in Windows and Linux.
The origins of this question are not about handling window closing
events, but about the proper way when running on a Mac to handle the
system-provided Quit and About menu items. If you took a Swing app you
designed on Windows or Linux and moved it to a Mac, you would find those
items present whether you wanted them or not. And the Quit menu item,
since your app wouldn't provide any handling for it, would simply
unceremoniously terminate your application without your intervention.
The About item would display a simplistic dialog with "Java" in it
rather than using your already-built code.
= Steve =

Signature
Steve W. Jackson
Montgomery, Alabama
> In an attempt to handle the special CMD+Q on a Mac that resides in the
> application menu I had to put in the following code:
[quoted text clipped - 22 lines]
> Thanks,
> Adam
I can't imagine why you're using toLowerCase, since the value of that
property when running on Mac OS X will be *exactly* "Mac OS X" on every
system.
In what way is it failing on Windows? Do you have code references
elsewhere (not shown here) to those Apple classes that are evaluated at
runtime on a Windows machine?
You might find more help from Apple via some useful web pages at
<http://developer.apple.com/referencelibrary/Java/idxPorting-date.html>.
On that page is a link entitled OSXAdapter that shows (with source code)
how to use reflection to hook these Apple classes into an application.
Just below it is one entitled Unsolicited About Boxes which shows more
on how to properly override the About and Quit behaviors. And just
below that is a link entitled AppleJavaExtensions where you can get stub
classes for compiling on other platforms. I would think that one or
more of those would provide useful answers.
= Steve =

Signature
Steve W. Jackson
Montgomery, Alabama
Rationem - 01 Apr 2006 17:59 GMT
The code below when run on a Mac works fine. It does not work on a
windows computer citing a class not found.
> > In an attempt to handle the special CMD+Q on a Mac that resides in the
> > application menu I had to put in the following code:
[quoted text clipped - 42 lines]
>
> = Steve =
Roedy Green - 01 Apr 2006 19:16 GMT
> com.apple.eawt.Application
is this something you invented or Apple did?
If you, you have no business using Apple's package name.
If Apple, that package appears to be Apple only. At the very least
you would have to arrange for the jar to be present.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Steve W. Jackson - 10 Apr 2006 21:47 GMT
> > com.apple.eawt.Application
>
[quoted text clipped - 3 lines]
> If Apple, that package appears to be Apple only. At the very least
> you would have to arrange for the jar to be present.
I have a much better idea. Refer back to my earlier posts on the
subject. Apple has public posted the information on how to do this
right, so that you have access to the stub classes for compiling on any
platform and making sure the Mac-specific code only executes on a Mac.

Signature
Steve W. Jackson
Montgomery, Alabama