tags:

views:

199

answers:

2

Hi,

I would like to use the JTable row sorter new in Java 6. But also I need it to be compatible in Mac OSX with Java 5.

Is it possible to find out the JVM version during runtime and use different code for the JTable with and without row sorter?

A: 
  1. You can find the JVM version in the System property "java.version".

  2. Copy the source of the JTable sorter object into your project and use that. Note that you're not allowed to distribute this unless you have a license from Sun. So this is okay for something which you only use yourself or within your company. Selling or putting it on the net for download is not okay. IANAL.

  3. Write a helper class which implements the sorter interface. In this class, put this code:

    Class sorterClass = Class.forName("javax.swing.table.TableRowSorter");

When this throws a ClassNotFoundException, then the class is not available. If it is, use Reflection on sorterClass to create an instance and install it on the table.

Note: You must not import any of the Java 6 classes anywhere! If you do, loading the helper class will fail. Also, you must compile the code with Java 5.

Aaron Digulla
+1  A: 

You could always use JXTable from the SwingX library instead. It has sorting built in.

Dan Vinton