• Post Reply Bookmark Topic Watch Topic
  • New Topic

confusion in TableModel(getRowCount method execute before constructor)  RSS feed

 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


my confusion is that getRowCount() method is executed before the constructor of MyTableModel therefore it gets data array as null. but as i know constructor should be executed first..
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that it will get called in the super constructor for the DefaultTableModel. But if you're providing your own data nucleus, I'm also thinking that you shouldn't be using DefaultTableModel which can lead to errors, since many of the methods that you call will refer to the Vector of Vectors held in the super, and so I'm thinking that you'll be better off here either passing your data in the DefaultTableModel's super by calling its constructor that does this on the first line of your constructor, or by instead extending the AbstractTableModel.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saurabh, you have earlier been advised to use code tags.
http://www.coderanch.com/t/493087/GUI/java/doing-same-job-two-or
Since you never revisited that thread, maybe you didn't see or follow the link.

In the same context, I would like to point you to another page in the FAQ:
http://www.coderanch.com/how-to/java/SayThanks
 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Saurabh, you have earlier been advised to use code tags.
http://www.coderanch.com/t/493087/GUI/java/doing-same-job-two-or
Since you never revisited that thread, maybe you didn't see or follow the link.

In the same context, I would like to point you to another page in the FAQ:
http://www.coderanch.com/how-to/java/SayThanks


thanks for your suggestion..... now i have edited my post and use code tag....
now please suggest me what's wrong in my code
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saurabh agr wrote:

thanks for your suggestion..... now i have edited my post and use code tag....
now please suggest me what's wrong in my code


I already have!
 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:
saurabh agr wrote:

thanks for your suggestion..... now i have edited my post and use code tag....
now please suggest me what's wrong in my code


I already have!



sorry i can understand what you want to say
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saurabh agr wrote:[
sorry i can understand what you want to say


Then please ask for clarification rather than imply that we haven't answered you. You are extending DefaultTableModel which uses an internal Vector of Vectors to store the data used by the JTable, but your class is ignoring this and instead using a 2-dimensional array of objects. This will confuse things since all of the methods of DefaultTableModel, such as addRow will use the super's Vector of Vector, not to your 2-dimensional array. I doubt that your data will even be displayed in the JTable that uses the model. If you want to continue to use DefaultTableModel, then get rid of your data and header variables, and instead on the first of line of your constructor call an appropriate super constructor (the DefaultTableModel's API will show you which are available) to put the data you want displayed in the super's data structure.

If on the other hand you have to use your own 2-dimensional array of vectors, then you should not extend DefaultTableModel, but instead should extend AbstractTableModel.

Bottom line: please read the API for both of the classes as well as the Swing JTable tutorial as it will explain all in better detail.

for e.g., this creates a DefaultTableModel override that holds your headers and a bunch of empty rows:
 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:
saurabh agr wrote:[
sorry i can understand what you want to say


Then please ask for clarification rather than imply that we haven't answered you. You are extending DefaultTableModel which uses an internal Vector of Vectors to store the data used by the JTable, but your class is ignoring this and instead using a 2-dimensional array of objects. This will confuse things since all of the methods of DefaultTableModel, such as addRow will use the super's Vector of Vector, not to your 2-dimensional array. I doubt that your data will even be displayed in the JTable that uses the model. If you want to continue to use DefaultTableModel, then get rid of your data and header variables, and instead on the first of line of your constructor call an appropriate super constructor (the DefaultTableModel's API will show you which are available) to put the data you want displayed in the super's data structure.

If on the other hand you have to use your own 2-dimensional array of vectors, then you should not extend DefaultTableModel, but instead should extend AbstractTableModel.

Bottom line: please read the API for both of the classes as well as the Swing JTable tutorial as it will explain all in better detail.

for e.g., this creates a DefaultTableModel override that holds your headers and a bunch of empty rows:


thanks a lot for your valuable suggestion and i m sorry for my mistakes please forgive me........ my confusion is now cleared........... thanks
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saurabh agr wrote:
thanks a lot for your valuable suggestion and i m sorry for my mistakes please forgive me........ my confusion is now cleared........... thanks


Glad it helped, and no need to apologize. Actually, if this is all my table model needed, then I wouldn't even subclass it, but rather use a DefaultTableModel object called with the appropriate constructor. But often I need to override one of the DefaultTableModel's methods, especially getColumnClass, and then I need to extend DefaultTableModel,
 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:
saurabh agr wrote:
thanks a lot for your valuable suggestion and i m sorry for my mistakes please forgive me........ my confusion is now cleared........... thanks


Glad it helped, and no need to apologize. Actually, if this is all my table model needed, then I wouldn't even subclass it, but rather use a DefaultTableModel object called with the appropriate constructor. But often I need to override one of the DefaultTableModel's methods, especially getColumnClass, and then I need to extend DefaultTableModel,


thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!