aspose file tools
The moose likes Swing / AWT / SWT and the fly likes confusion in TableModel(getRowCount method execute before constructor) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "confusion in TableModel(getRowCount method execute before constructor)" Watch "confusion in TableModel(getRowCount method execute before constructor)" New topic
Author

confusion in TableModel(getRowCount method execute before constructor)

saurabh agr
Ranch Hand

Joined: Apr 14, 2010
Posts: 35


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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: May 03, 2008
Posts: 4167
    
    3

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


luck, db
There are no new questions, but there may be new answers.
saurabh agr
Ranch Hand

Joined: Apr 14, 2010
Posts: 35
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Apr 14, 2010
Posts: 35
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Apr 14, 2010
Posts: 35
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Apr 14, 2010
Posts: 35
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion in TableModel(getRowCount method execute before constructor)
 
Similar Threads
JTable
JTable
Problem with JTable
jtable
Swing--JTable in JInternalFrame