tags:

views:

1090

answers:

5

Is it possible to port a UI developed using Swing in Java 1.6 to Java 1.5 without rewriting all again?

+3  A: 

If you've only used features that are common between the two, I'd say yes. It should be backwards compatible. The moment you add JDK 6 specific features you have to take those out.

duffymo
+7  A: 

Just to elaborate on what duffymo said:

Java is designed to be backward compatible. I.e. if you write something in 1.5 you can run it in 1.6. Of course there are limits to that (e.g. new reserved words like assert break it) but in general it works fine.

If you go the other way, it's a bit more dangerous. You certainly won't need to rewrite all your code, but you might have used features only available since 1.6. Such features can be new classes or new methods of existing classes.

Another thing is the binary code level. You can set your compiler running 1.6 to create code for 1.5 or even 1.4.

But beware, that your 1.6 compiler most certainly compiles against a 1.6 rt.jar. That means you won't notice missing classes or methods until you actually run or compile your code with 1.5.

Setting the compliance level only means that the 1.5 JRE will understand the code, not that all "default" classes are available.

I don't have any experience with 1.6 specifics, but i think while 1.5 added a lot of new language features (that are not all compatible with 1.4), 1.6 was more of a maintenance release.

Stroboskop
+1  A: 

Since Swing didn't change much in the last ten years, you should not run into many problems. Java 6 also hasn't many new features over Java 5 (mostly internal cleanup).

I suggest to just try to compile your app with Java 5 and run it. It might work out of the box.

Aaron Digulla
Swing in JDK 6 has some additions like support for system notification area, baselines, group layout, ... fortunately, most of them were integrated from external libraries which should still be available.
Peter Štibraný
If all else fails, you might be able to extra the classes for Swing 6 and run them under Java 5. Unless they use DLLs, of course. But I found that some some problems in Java, a brute force approach works pretty well.
Aaron Digulla
I am not sure if this would work in swing case. There are new methods in JComponent (like getBaseline), which are used by Group layout. If you only extract group layout, it won't work. And replacing entire Swing ... well, maybe you can make it work, but it won't be easy due to package sealing.
Peter Štibraný
Aaron Digulla
+3  A: 

As problem is with group layout, here are some pointers.

Older version of Group layout code is still available at http://swing-layout.dev.java.net/ ... this isn't latest version as found in JDK 6, but maybe it will work for you? (In case you cannot find it, here is link to latest release: https://swing-layout.dev.java.net/servlets/ProjectDocumentList?folderID=8136&expandFolder=8136&folderID=0)

And if you're using NetBeans, here are steps to configure Netbeans to use external group-layout for deploying on jdk5: http://blogs.sun.com/NetBeansSupport/entry/target_jdk

Peter Štibraný
A: 

If you want to assure your code runs in a 1.5 environment, set your IDE to to build against the 1.5 JDK, that way you won't inadvertently use any of the new API.

Mike Katz