This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes java.lang.ClassCastException: javax.swing.KeyStroke cannot be cast to java.lang.Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "java.lang.ClassCastException: javax.swing.KeyStroke cannot be cast to java.lang.Comparable" Watch "java.lang.ClassCastException: javax.swing.KeyStroke cannot be cast to java.lang.Comparable" New topic
Author

java.lang.ClassCastException: javax.swing.KeyStroke cannot be cast to java.lang.Comparable

annapoorna chitturi
Greenhorn

Joined: Jul 03, 2012
Posts: 1
The SWING frame work that i am using is working fine with jre1.6.When i try to run it through jre1.7 getting the below exception .

java.lang.ClassCastException: javax.swing.KeyStroke cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)

Can any one help to reslove this ..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Welcome to the Ranch

You know that a class cast can only cast an object to a type that it already is. If you look up KeyStroke, you find it doesn’t implement Comparable. I can tell that without looking in the API from the exception message you quoted. You may be able to sort by using the getKeyChar or getKeyCode methods, but I don’t know. You might have to use those methods inside the compare() method of a Comparator<KeyStroke>.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

It might help if you explain why, when you want to map KeyStrokes, you choose to use TreeMap and not some other Map implementation.

Also, I can't see where the Java version should make a difference. Can you post the code that you say works in Java SE 6 but fails in 7, in the form of an SSCCE?


luck, db
There are no new questions, but there may be new answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java.lang.ClassCastException: javax.swing.KeyStroke cannot be cast to java.lang.Comparable
 
Similar Threads
PriorityQueue ,add and compare object
java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable
Question 10 Chapter 7 : K and B
distributed banking code , getting error : _stub class cannot be casted!!!
Comparator