I should have mentioned, it's the Java implementation on OSX where the problem occurs. JTable is the Java implementation of an emulated native table control.
As revealed to me on the [email protected] list, there are some client properties that can be set to specify specific Cocoa behaviors in the Java implementation of certain JComponent classes. The properties are listed and their usage explained here.
http://developer.apple.com/mac/library/technotes/tn2007/tn2196.html#GENID1
An Apple engineer pointed out that these are workarounds, as OSX should work out of the box same as Windows. I'll submit a bug report, per his suggestion.
Here's my implementation of the fix for the problem I encouontered. The following method is called from the JTable constructor, so the methods invoked in it are JTable methods.
private void setupRowSorter() {
setAutoCreateRowSorter(true);//Java6 only. For Java 5, create a TableRowSorter and add it to the JTable
getTableHeader().addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
getTableHeader().putClientProperty("JTableHeader.selectedColumn", getTableHeader().columnAtPoint(e.getPoint()));
if (getTableHeader().getClientProperty("JTableHeader.sortDirection") == null) {
getTableHeader().putClientProperty("JTableHeader.sortDirection", "ascending");
} else if (getTableHeader().getClientProperty("JTableHeader.sortDirection").equals("ascending")) {
getTableHeader().putClientProperty("JTableHeader.sortDirection", "decending");//misspelling is required, per Apple documentation
} else {
getTableHeader().putClientProperty("JTableHeader.sortDirection", "ascending");
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
}