S
softwarepearls_com
In Collator, there's the following method:
public int compare(Object o1, Object o2) {
return compare((String)o1, (String)o2);
}
I would have thought that using toString() instead of a String cast
would be a bit more robust and flexible:
public int compare(Object o1, Object o2) {
return compare(o1.toString(), o2.toString());
}
As a result of the above, I'm currently seeing the following
ClassCastException when trying to sort a JTable column holding
Integers:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
java.lang.Integer cannot be cast to java.lang.String
at java.text.Collator.compare(Collator.java:310)
at javax.swing.DefaultRowSorter.compare(DefaultRowSorter.java:951)
at javax.swing.DefaultRowSorter.access$100(DefaultRowSorter.java:95)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:
1359)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:
1349)
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.sort(Arrays.java:1079)
at
javax.swing.DefaultRowSorter.sortExistingData(DefaultRowSorter.java:
536)
at javax.swing.DefaultRowSorter.setSortKeys(DefaultRowSorter.java:
304)
at javax.swing.DefaultRowSorter.toggleSortOrder(DefaultRowSorter.java:
463)
at javax.swing.plaf.basic.BasicTableHeaderUI
$MouseInputHandler.mouseClicked(BasicTableHeaderUI.java:93)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:
253)
at java.awt.Component.processMouseEvent(Component.java:6137)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:
4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:
3974)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:
269)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:
184)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:
174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
(BTW: note the telltale signature in this trace of a recursive
method.. in this case Arrays.mergesort)
... Had Collator used the toString() approach, then this silly
exception could be avoided. It's just silly for Swing not to be able
to sort JTable rows on columns holding Integers.
Any ideas why Collator.compare has to be implemented using these
casts?
public int compare(Object o1, Object o2) {
return compare((String)o1, (String)o2);
}
I would have thought that using toString() instead of a String cast
would be a bit more robust and flexible:
public int compare(Object o1, Object o2) {
return compare(o1.toString(), o2.toString());
}
As a result of the above, I'm currently seeing the following
ClassCastException when trying to sort a JTable column holding
Integers:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
java.lang.Integer cannot be cast to java.lang.String
at java.text.Collator.compare(Collator.java:310)
at javax.swing.DefaultRowSorter.compare(DefaultRowSorter.java:951)
at javax.swing.DefaultRowSorter.access$100(DefaultRowSorter.java:95)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:
1359)
at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:
1349)
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.sort(Arrays.java:1079)
at
javax.swing.DefaultRowSorter.sortExistingData(DefaultRowSorter.java:
536)
at javax.swing.DefaultRowSorter.setSortKeys(DefaultRowSorter.java:
304)
at javax.swing.DefaultRowSorter.toggleSortOrder(DefaultRowSorter.java:
463)
at javax.swing.plaf.basic.BasicTableHeaderUI
$MouseInputHandler.mouseClicked(BasicTableHeaderUI.java:93)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:
253)
at java.awt.Component.processMouseEvent(Component.java:6137)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:
4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:
3974)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:
269)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:
184)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:
174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:
161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
(BTW: note the telltale signature in this trace of a recursive
method.. in this case Arrays.mergesort)
... Had Collator used the toString() approach, then this silly
exception could be avoided. It's just silly for Swing not to be able
to sort JTable rows on columns holding Integers.
Any ideas why Collator.compare has to be implemented using these
casts?