Mac Java

H

hinz.adam

I am building a java application that will be run on Mac and Windows
and I have run into a bit of a snag. The main problem I have is that
Macs seem to automatically insert the application menu and that menu
does not appear in java.

When the user hits Apple+Q (on the Mac) the program exists immediatly,
without giving java a chance to prompt the user to save his or her
data.

What can I do to handle this event and still keep my application
cross-platform?

Thanks,
Adam
 
T

Thomas Fritsch

I am building a java application that will be run on Mac and Windows
and I have run into a bit of a snag. The main problem I have is that
Macs seem to automatically insert the application menu and that menu
does not appear in java.

When the user hits Apple+Q (on the Mac) the program exits immediatly,
without giving java a chance to prompt the user to save his or her
data.
I'm not familiar with Java on Mac. But I assume Apple+Q (on the Mac) is the
equivalent of Alt+F4 (on MS-Windows).
How does your java application react on Windows when you hit Alt+F4 ?
I assume it exits immediately, without giving java a chance to prompt the
user to save his/her data.
What can I do to handle this event and still keep my application
cross-platform?
If my two assumptions above are correct, then you can handle these events in
a cross-platform way. I vaguely remember, that you have to do something
like this:
yourFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
yourFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (....)
// pop up a message "Do you want to save?"
else
System.exit(0);
}
});

You should find the details by searching the groups comp.lang.java.* for
"Alt+F4" and "setDefaultCloseOperation".
 
H

hinz.adam

ALT+F4 Works as it should on windows but Apple-Q does not on the mac I
believe that they are seperate mechanisms.
 
S

Steve W. Jackson

Thomas Fritsch said:
I'm not familiar with Java on Mac. But I assume Apple+Q (on the Mac) is the
equivalent of Alt+F4 (on MS-Windows).
How does your java application react on Windows when you hit Alt+F4 ?
I assume it exits immediately, without giving java a chance to prompt the
user to save his/her data.
If my two assumptions above are correct, then you can handle these events in
a cross-platform way. I vaguely remember, that you have to do something
like this:
yourFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
yourFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (....)
// pop up a message "Do you want to save?"
else
System.exit(0);
}
});

You should find the details by searching the groups comp.lang.java.* for
"Alt+F4" and "setDefaultCloseOperation".

I'm afraid your assumptions are not valid. Alt-F4 is a "close window"
command on Windows. The "Apple" key is properly referred to in the Mac
community as the Command key, and Command-Q is the common keyboard
equivalent for exiting an application, analogous to File/Exit in most
Windows applications. Where Windows uses the Control key for many
keyboard equivalents (Ctrl-C, Ctrl-X, etc.), the Mac often uses the
Command key instead (Command-C, Command-X, etc.).

Mac OS X automatically provides every application with an "application
menu" bearing the application's name. This menu includes an About menu
item and a Quit menu item (with Command-Q assigned as its shortcut, in
Java terms). This is what the OP is bumping up against.

It would be a good idea to visit Apple's own Java pages. I would start
with <http://www.apple.com/java/>, which will redirect to a page with
info on Apple's included Java. The latest OS X includes Java 1.4.2
already installed and 1.5.0 is available. Apple's 1.5 API is at
<http://developer.apple.com/documentation/Java/Reference/1.5.0/appledoc/a
pi/index.html>, listing Apple's extensions. For the core API, they link
to Sun's own site since Apple's JVM is compliant. Developer information
is at <http://developer.apple.com/java/>, where documentation links can
be found.

I haven't yet had the opportunity to experiment with making my work app
(currently offered for Windows and Linux) fit into the Mac world. But
to do so, I would suggest looking at the information in the API on the
com.apple.eawt.Application class as a start. The comments at the top
address some of the information that has to be understood in order to
make an app fit into the Mac way of doing things. The unstated but
obvious parts include not providing a File/Exit menu on the Mac, or a
Help/About item, but instead attributing their behaviors to the ones
provided. And there's other info available on some properties that will
be helpful to cause the application to use a proper name for the
Application menu (rather than "java" or a qualified package.class name).

HTH.

= Steve =
 
T

Thomas Fritsch

Steve said:
Thomas Fritsch said:
I'm not familiar with Java on Mac. But I assume Apple+Q (on the Mac) is
the equivalent of Alt+F4 (on MS-Windows).
[...my obsolete stuff snipped...]

I'm afraid your assumptions are not valid. Alt-F4 is a "close window"
command on Windows. The "Apple" key is properly referred to in the Mac
community as the Command key, and Command-Q is the common keyboard
equivalent for exiting an application, analogous to File/Exit in most
Windows applications. [...]
Apple's 1.5 API is at
<http://developer.apple.com/documentation/Java/Reference/1.5.0/appledoc/a
pi/index.html>, listing Apple's extensions.
[...]
Thanks for pointing me into the right direction. I didn't expect Apple's
Mac-Java to be so much different (or should I say "better" ?) than Sun's
Windows-Java.
 
H

hinz.adam

I found the MRJ Interfaces that allow for catching the event but those
don't exist on the windows JVM so if I used those wouldn't i have to
package two different versions? One for mac that includes references to
the MRJ interfaces and one for windows that doesn't?

thanks
Adam
 
S

Steve W. Jackson

Thomas Fritsch said:
Steve said:
Thomas Fritsch said:
I'm not familiar with Java on Mac. But I assume Apple+Q (on the Mac) is
the equivalent of Alt+F4 (on MS-Windows).
[...my obsolete stuff snipped...]

I'm afraid your assumptions are not valid. Alt-F4 is a "close window"
command on Windows. The "Apple" key is properly referred to in the Mac
community as the Command key, and Command-Q is the common keyboard
equivalent for exiting an application, analogous to File/Exit in most
Windows applications. [...]
Apple's 1.5 API is at
<http://developer.apple.com/documentation/Java/Reference/1.5.0/appledoc/a
pi/index.html>, listing Apple's extensions.
[...]
Thanks for pointing me into the right direction. I didn't expect Apple's
Mac-Java to be so much different (or should I say "better" ?) than Sun's
Windows-Java.

I wouldn't say that the Java is any different or better. In some
circles, there are those who complain that it's slower and more
cumbersome on a Mac. But I do applaud the fact that they provide
relatively easy means of letting a Java app adapt to the traditional UI
that Mac users are accustomed to using. In 1.3.1 they had a separate
API, though I don't know what was different beyond extensions like
these. As of 1.4.2 and 1.5 they've kept it simpler, only using the
extensions and documenting some problems and/or quirks.

= Steve =
 
S

Steve W. Jackson

I found the MRJ Interfaces that allow for catching the event but those
don't exist on the windows JVM so if I used those wouldn't i have to
package two different versions? One for mac that includes references to
the MRJ interfaces and one for windows that doesn't?

thanks
Adam

You don't need different versions at all. You only need a small amount
of logic that checks the os.name property for "Mac OS X" and adds those
hooks. At runtime, those classes will be present on a Mac. At compile
time, you'll need to create some code on the Mac (unless Apple makes
them available -- I can't say since I haven't gone there yet). Just
make sure that no runtime code on any other platform refers to those
classes.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top