Forums Register Login

Object : Cosmic Super Class

+Pie Number of slices to send: Send
I have an application in which I am using a JTable which displays lots of custom objects as row data.
Since I wanted to override only the text representation of these objects, I set a default renderer for the Object class, hoping that it would work just fine.
However, I observed it was not working correctly for the classes like Long and Integer. When I peeked into the source code, I realised that the Number class does NOT extend the Object class.

Anyone know why this is? I was under the impression that Object is the cosmic super class.
+Pie Number of slices to send: Send
 

Originally posted by Maneesh Godbole:
When I peeked into the source code, I realised that the Number class does NOT extend the Object class.



It does, implicitly.

That is, when the compiler doesn't find an extends clause in the class declaration, it automatically adds an "extends java.lang.Object" for you.
+Pie Number of slices to send: Send
 

Originally posted by Ilja Preuss:


It does, implicitly.

That is, when the compiler doesn't find an extends clause in the class declaration, it automatically adds an "extends java.lang.Object" for you.



But, my renderer did not work for Long. Any idea why? I had set it to Object.class
+Pie Number of slices to send: Send
As a "proof", execute

System.out.println(Number.class.getSuperclass());
+Pie Number of slices to send: Send
 

Originally posted by Maneesh Godbole:

But, my renderer did not work for Long. Any idea why? I had set it to Object.class



JTable automatically registers renderers for some classes (see the source code of JTable.createDefaultRenderers). And it uses the most specific renderer it can find for a column. So if you want those classes to be rendered by the renderer registered for Object, you will have to delete the more specific ones by setting them to null. For example:

myTable.setDefaultRenderer(Long.class, null);
You learn how to close your eyes and tell yourself "this just isn't really happening to me." Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2551 times.
Similar Threads
abstract class and Interface
Cast -- Runtime exception - marcus green exam
Object Serialization
Generic Question in JAVA Beat
How many Objects get Serialized
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:26:05.