Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Subclassing JTable

 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to subclass JTable to use in my application. The Table works correctly, but I want to add Event functionality. I tried to add the line
this.addTableModelListener(); to this class but it gave a compiler erro. I tried to add the same line to my JFrame clas used to display the instantiated JTable and it did not work.


 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our Swing/AWT/JFace/SWT forum.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside your constructor you would write

Elsehere you would write


That being said, your design looks bad. First, do you *really* need to subclass JTable?
What JTable method are you overriding? Second, this sort of typing is bad:

because one can do the following with it:

Did you really intend to have one table listen to another? I suspect you wanted
to define a table that needed, solely as an implementation detail, to define a TableModelListener.
In that case, I would define a seperate class implementing TableModelListener, or
if you really need it to access implementation details of Table, to do this:

[ November 02, 2005: Message edited by: Jeff Albrechtsen ]
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a subclass of JTable to take some of the design code out of my JInternalFrame class which displays the Table. Thanks for all of your suggestions.
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your suggestion in my code, and none of it works as you suggested.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you're doing it wrong:
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I will give it a try. I would simply like to be able to re-edit the JavaBean values in my JTable and have those values be transmitted back to the JavaBean via the set methods i have designed in my FilmBean class.

Thanks for your advice on subclassing. it seems that it is not a good idea to do it if one can avoid it.

I have about 60 lines of code in my JTable subclass. it seemed to make sense to keep it in a separate class and instantiate it in the class that will display it like so Table table = new Table(model);
All of the configuration is taken care of in the subclass. Apparently alot of java people are dead set against this approach. I don't have enough experience to understand it or grasp why..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic