Coding in Java on Mac OSX
OSX has fantastic integration with Java. You'd hardly be able to tell the difference between an ObjectiveC app and a Java app. I've found a bunch of good sources for info on how to make Java apps more Mac-like, so I decided to make a webpage, mainly to remind myself more than anyone else! As you can tell from the other pages on this site, my Mac OSX apps are written in Java.
GUI Notes
> Drop-Zone Widget
This is a simple widget to allow java coders to create JPanels that mimick the so-called drop zone from the OSX Aqua interface. I was unhappy with the version I found in Apple's ImageBorder class, so I altered the image slightly and added a static method to make it easier to make something into a drop zone (I'm not sure what to call it, but that's what their ImageBorder refers to it as). Check the images below for the difference - I lightened up the rounded edges and also made the very corners transparent. Yes, I am rather picky about such things :-)
download it now
You can also view the source (the methods I added are on the very bottom), and see my updated background image, To run it just do the following on the command line:
      javac ImageBorder.java
      java ImageBorder

the original my version
Apple's Version My Version
> MRJApp.properties
You should set all these properties in your MRJApp.properties file inside of your application package. A full list of the properties with more info can be found on Apple's developer site.
com.apple.macos.useScreenMenuBar=true

This property sets the application to put the menu-bar at the top of the screen, instead of inside a window. This gives the app a Mac-like feeling to it. Set it to true or false.
com.apple.macos.use-file-dialog-packages=true
JFileChooser.packageIsTraversable=never
JFileChooser.appBundleIsTraversable=never

These shows application packages as files in the file open and close dialog boxes. On a mac, it makes more sense to show it as a file, rather than as a folder, just like in the Finder.
com.apple.mrj.application.apple.menu.about.name=My Application

On any OSX before v10.2 (Jaguar), you NEED to have this in order for the about menu to show up. Set it to the name of the application and register the MRJAboutListener to use it.
File Management
> File-Type and Creator Codes
On the mac, applications have a unique 4-character identification code. You can register one on apple's site. Then when you save a file, use the com.apple.mrj package to set the file's creator and file type. This is useful so that later if you double-click on the file it will open with that application:
File f = new File("example");
MRJOSType myCreatorCode = new MRJOSType("Jakl");
MRJOSType myFileType = new MRJOSType("logo");
MRJFileUtils.setFileCreator(f, myCreatorCode);
MRJFileUtils.setFileType(f,myFileType);
> End-Of-Line Characters
It's a big pain in the rear that Macs, Unix, and PCs all use different characters to signify the end of a line. Some of these harken back to the days of typewriters, with carriage return (CR) and line feed (LF) being the characters used. Often you can avoid these, as more applications become sensitive to this fact, but if not check out Gregory Guerin's Text-Lines Toolkit for Java. His Notifications for Java is also useful - it allows you to make your applications icon bounce in the dock, among other things.
> Preferences
You can also use the the com.apple.mrj package to find out the correct place to write your preferences file. I haven't found a package that writes .plist files yet (for OSX), but maybe I or someone else will write one soon:
String macPrefsLoc = MRJFileUtils.findFolder(
      MRJFileUtils.kPreferencesFolderType).getPath();
File prefsFile = new File(macPrefsLoc,"MyPreferences.txt);
Serial Port Stuff
> USB->Serial Adaptors
There are a number of USB->Serial adaptors for all the new Macs that don't have serial ports. It's too bad you need them, since serial ports are just so useful for hacking, and USB is a huge pain in the ass. Anyway, I use the Keyspan adaptors. They have a driver for OSX.
> JavaComm API
You'll need to use the JavaComm API. However, there is no official implementation for OSX. So I use a release from the good guys at RXTX - it works great. Unforunately you have to dig through their huge CVS tree to find the installer, so I'm hosting the JavaComm for OSX installer v2.1-6 here.
Links
> Free Software by Gregory Guerin
Nice libraries for doing mac-specific things.
> Tailoring Java applications for Mac OSX
A thorough tutorial from Apple's Developer site.
> Java runtime properties for Mac OSX
A reference for all the properties you can set on Mac OS X.


:: go back to Rahul's tech notes ::