Mixing Java and Titanium: Part Three
Pages: 1, 2
Swing apps on Mac OS X: It's all good and getting better
The most frequent question people have for me when I tell them that I am developing Java on Mac OS X is, "How well do your applications run?" And the answer is that my experiences have been really good -- stunningly good in fact. But then, most of my applications have been faceless server applications and most of the cross-platform problems in Java hit GUI applications. As luck would have it, a few of my friends that develop GUI applications have started developing on Mac OS X, as well and I have good news to report.
|
|
One of these friends, Greg Murray, has been working on his own Java code editor for the past year or two in his spare time -- that is, when he is not working on the J2EE Blueprints at Sun. As he makes improvements, it is turning into quite a decent tool for hacking on Java code. You can find this program, called MightyJ, at http://www.mightyj.com/. Greg originally developed this application on Linux and Windows, but after seeing my OS-X-based laptop and its JDK 1.3 implementation, he took the plunge and bought a Cube and started hacking on MightyJ using OS X. And his experience was almost seamless. They only place he had to change code was in some custom components he built himself. A few days later, he was raving about how MightyJ looked just like a native OS X app and was impressed by the lack of problems when running it on OS X. Now, his biggest complaint is that he doesn't have a Mac OS X machine at work.
You can find screenshots of MightyJ running on OS X at http://www.mightyj.com/screen_shots.html.
Even better, it seems that Swing on OS X is just going to get better. Apple has been hard at work at figuring out how to get hardware acceleration behind Swing to drastically improve performance and has been showing this technology off at Apple's Worldwide Developers Conference (WWDC). In demos, it turned previous choppy resize operations into silky smooth ones. This technology will be shipping as part of the upcoming Java update, but will not be turned on by default as it is not quite ready for primetime. You can bet that I'm digging in to find more information about this technology and will get details out as soon as I can.
A bit more Objective-C from a Java point of view
Earlier, I discussed a few of the differences between Objective-C and Java. Now I'm going to take a look at a couple of things that are annoying, and sometimes painful, about working in Objective-C. Especially when coming from a Java background.
First, Objective-C is based on the strict ANSI C standard. This means that you are limited to declaring new variables to the top of a block of code. What happens in practice is that the following code (a snippet that all Java programmers have used a bazillion times and have automatically programmed into their fingertips) will not compile:
for (int i = 1; i < j; i++) {
// do stuff
}
Instead, you need to write your code to look something like:
int index;
for (index = 1; index < j; index++) {
// do stuff
}
This looks innocent enough in the example above, but most methods have a page or two (hopefully not more!) of implementation. In real life, the example looks more like the following:
int index;
// Lots of code in method...
[[NSColor whiteColor] set];
NSRectFill(rect);
for (index = 1; index < j; index++) {
// do stuff
}
Declaring an index a page before the for loop that uses it is quite annoying. Or declaring any short-lived variable too far from where it is being used is a sure way to run into readability problems. At WWDC, it was mentioned that this problem will likely go away as the C support is upgraded. Until that happy day, there is a workaround. Simply open a new block of code with curly braces. Here's an example:
// previous code in method...
[[NSColor whiteColor] set];
NSRectFill(rect);
{
int index;
for (index = 1; index < j; index++) {
// do stuff
}
}
The second issue is one that is not picked up the by the compiler. It is the accidental case of using an `=` assignment operator in an if(condition){} statement instead of the `==` comparison operator. There have been at least three cases in the last few days where the following code caused non-obvious problems in my programs:
if (answer = NSAlertDefaultReturn) {
sp = [NSSavePanel savePanel];
[sp setRequiredFileType:@"expenses"];
answer = [sp runModal];
// ...
}
The above code will compile just fine, but trying to figure out what is happening at runtime can get a little tricky. Even if you have a hint of the problem, having to scan pages of code looking for a single missing '=' character can make your eyes cross.
Are these issues enough to keep me from further experimentation with Objective-C? Nope, not at all. But they do constantly annoy me as I explore the language and the Cocoa frameworks. They take a little bit of the shine off an otherwise enjoyable experience. Just enough to make me really appreciate some of the polish that the creators of Java put into the language and its semantics.
James Duncan Davidson is a freelance author, software developer, and consultant focusing on Mac OS X, Java, XML, and open source technologies. He currently resides in San Francisco, California.
Return to the Mac DevCenter.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 11 of 11.
-
Why use "Cocoa Java"? Why not just plain old "Cocoa" or JDK 1.whatever and Swing?
2003-08-09 09:50:08 anonymous2 [Reply | View]
-
Anybody exploring server side java on OS X?
2001-12-26 13:41:57 lworeilly [Reply | View]
I'm a java developer who doesn't own an OS X machine (yet) but am curious about the potential. This article series (1-3 so far) has been helpful in showing me some of what I should expect in the J2SE implementation.
Question:
What about J2EE? I see that apache is included with OS X. Any feedback on whether Tomcat and JBoss will run ok on this platform? What about mySql or Oracle? Any pointers to further info appreciated.
Thanks,
LW
-
Is FORTE ever going to available on OS X?
2001-11-14 13:04:36 fernieds [Reply | View]
Is sun FORTE IDE ever going to available on OS X
the way it is for the evil empire that is microsoft ?
-
Documentation for Java Developers
2001-10-13 16:07:40 windsurfer [Reply | View]
With all the Java developers available out there you'd think that there would be more documentation and tutorials released with the developer tools. It's a tremendous barrier to entry for novices. "Not such a bad thing" you might say, but the seeds of the future should be sown in fertile ground, not barren.
as Davidson says:
"But, after that point, the remaining Java-oriented documentation consists mainly of the Application Kit and Foundation API references. And worse, these references are incomplete ports of the Objective-C documents. For example, the following sentence comes from the Java application references for the NSEvent class:
"NSWindow and NSApplication define the method nextEventMatchingMask:untilDate:inMode:dequeue:, which allows an object to retrieve events of specific types."
The above makes very little sense to a Java programmer unless they have some experience with Objective-C message-naming conventions."
-
Why use "Cocoa Java"? Why not just plain old "Cocoa" or JDK 1.whatever and Swing?
2001-10-01 10:35:39 smithkennedy [Reply | View]
This is not meant to antagonize anybody. I am genuinely curious. I don't undestand why somebody would want to do this.
I had originally thought that the Java Cocoa bindings were created by Apple to allow Java or C++ programmers to more easily author native binary programs using the Cocoa framework, but allow them to leverage their familiarity with Java/C++ syntax. But from what I understood from talking with people at WWDC, a program written in Java using the Cocoa APIs does not compile into a native binary. Instead, it compiles into the usual Java bytecodes. The Cocoa Java bindings are simply JNI interfaces into the Cocoa framework. But this program has been written using a platform-dependent Java API, which means it isn't portable to other platforms. So you end up with a program that is tied to one platform, but doesn't execute natively on that platform.
I fail to see any advantage to writing a program in this way. If I am a skilled Java programmer, why would I not just write my program using whatever version of the JDK makes sense? I already know the APIs / frameworks available to me - why learn something new that isn't portable. And, alternatively, if I am willing and able to learn Objective-C, why wouldn't I learn the Cocoa framework using the language it was originally implemented in?
As I said, I am not trying to kick sand in anybody's eyes here. I just have never heard anybody demonstrate why this would be a good engineering decision. I come from a background of 5 years of Java development experience, and I have recently been trying to address what framework I should learn to develop UI on Mac OS X. Code portability is definitely important.
Thanks for any info. -
Why use "Cocoa Java"? Why not just plain old "Cocoa" or JDK 1.whatever and Swing?
2003-12-05 01:07:30 maerics [Reply | View]
Interface Builder. In half an hour you can create a polished, intuitive GUI that plugs right in to your code. And, if you use the MVC paradigm well you can reuse the model objects and just write new controllers for, say, a pure java GUI.
But I've got to agree with you. JNI is "Java" only in language, not in portability. So the only real reason to use Cocoa java is because you want to write a serious application for Mac OS X and are more comfortable with Java than with Objective-C. But of course, that's not so strong a justification until Apple comes up with JNI wrappers for all of the frameworks (Security I'm looking at you).
-mike -
Why use "Cocoa Java"? Why not just plain old "Cocoa" or JDK 1.whatever and Swing?
2002-06-04 21:58:54 kastork [Reply | View]
I think Apple developed this whole system to enable the use of Interface Builder. Goal being -- make it easy to make Mac OSX programs using Java, as opposed to making Java programs that happen to run nice on OSX.
-
OS 10.1 and WebObjects
2001-09-28 04:46:29 tripodsblitz [Reply | View]
Hi,
Steve Jobs stated that there are three commandments for 10.1: "speed, speed, speed", so it is quite likely that the Swing performance has improved as well.
I saw his key-note at the WWDC and I was impressed by the speed of 10.0 anyway.
If we're talking about Java I just wannted to point out that with OS X there comes a free license of WebObjects, a supreme Application Server that let you not only develop Desktop Apps with the tool "Project Builder", but also develop professional (!) Web Apps with HTML or Swing Gui.
And best of all, it has a persistence framework that handles all the nasty database operations and caching for you. Get your hands on it, it's pretty cool.
Beside that, I read that the J2EE server ORION (the one that Oracle licensed as their so called "OC4J" server) is running pretty well under Mac OS X.
Cheers,
Markus







However, I don't want to have to learn ProjectBuilder to do that, I've been using Eclipse for a while on MacOS X, Windows (various) and Linux with few ill affects, and I just lack time for one more IDE (that's also why I like Java in general).
Some good, simple, direct examples that use the NS interface toolkit from Java would go a long way towards filling the void I think. I can then wrap them in Reflection so that the final program is portable and only does Mac stuff on a Mac.