Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Object : Cosmic Super Class  RSS feed

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a "proof", execute

System.out.println(Number.class.getSuperclass());
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!