Używam modelu, gdzie podaję odpowiednie obiekty:
@Override
public Object getValueAt(int i, int j) {
Object o;
switch (j) {
case 0:
o=visits.get(i).getNameVisit(); //string
break;
case 1:
o=visits.get(i).getDate(); //Date
break;
default:
o=null;
break;
}
return o;
}
Korzystam z ułatwienia sortowania:
tabVisit.setAutoCreateRowSorter(true);
Pierwsza kolumna ze stringami bez problemu się sortuje natomiast druga rzuca wyjątkami, jak próbuję ją sortować:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String
at java.text.Collator.compare(Unknown Source)
at javax.swing.DefaultRowSorter.compare(Unknown Source)
at javax.swing.DefaultRowSorter.access$100(Unknown Source)
at javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)
at javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at javax.swing.DefaultRowSorter.sort(Unknown Source)
at javax.swing.DefaultRowSorter.setSortKeys(Unknown Source)
at javax.swing.DefaultRowSorter.toggleSortOrder(Unknown Source)
at javax.swing.plaf.basic.BasicTableHeaderUI$MouseInputHandler.mouseClicked(Unknown Source)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Mamy błąd rzutowania: java.util.Date cannot be cast to java.lang.String
Jednak nie rozumiem z jakiego powodu Date nie może być zamieniony do Stringa skoro ma metodę .toString() i wyświetla się w tabeli poprawnie.
Dodatkowo Date implementuje interfejs Comparable, więc z sortowaniem nie powinno być problemu.
Po zamienieniu w modelu kodu na:
o=visits.get(i).getDate().toString();
Oczywiście sortowanie działa, ale sortuje jak Stringa, a wyświetla tak jak wcześniej.
Nie rozumiem dlaczego po podaniu Date nie sortuje się automatycznie tylko wyjątkiem rzuca.