1,105,271 Community Members

Set JTable rowCount

Member Avatar
pspwxp fan
Junior Poster
135 posts since Feb 2009
Reputation Points: 1 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

I have a JTable with two columns in my JFrame. The number of rows this table has should equal a number input in a JForm in a previous JFrame. I cannot find any rowCount mutator function (Using Netbeans), and upon further inspection, the rowCount property in the GUI design view is apparently locked. How do i go about my task?

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

The row count tells you the number of rows in yout table's model, just like length() tells you the number of chars in a String. It makes no sense to set it, because then it would be wrong. Just make your model the right size.

Member Avatar
pspwxp fan
Junior Poster
135 posts since Feb 2009
Reputation Points: 1 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

The number of rows has to be dynamically set. Like i said, it depends on a value you enter in a previous form. It takes a quantity, and displays a table for entering details for those items.

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Just make your model the right size.

Member Avatar
pspwxp fan
Junior Poster
135 posts since Feb 2009
Reputation Points: 1 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

So there is absolutely no way to dynamically set the number of rows for a table in Java?

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

I don't know how to make this any simpler for you. Of course you can dynamically set or change the number of rows in your table. Have you read the API documentation for JTable, or the tutorial which it links to? You just make your model the right size.

JTable is just a view of an underlying TableModel. It's the TableModel that defines the number of rows and columns. You create your table model with whatever number of rows you want.
For example, have a look at the constructors for DefaultTableModel

Member Avatar
pspwxp fan
Junior Poster
135 posts since Feb 2009
Reputation Points: 1 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

If I right-click the table, and go to "Table Contents", I come to a window where I can edit the table model, rows, columns, etc. However I still see no way to make the program decide the number of rows during run-time. I tried checking "Custom Code" and added

"new DefaultTableModel(Integer.parseInt(jTextField1.getText()), Integer.parseInt(jTextField2.getText()))

But its throwing errors, and understandably so since what if the user enters non-numerical text. And handling exceptions is not possible since the code for this is auto-generated and uneditable.

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

You have hit the limits of what automatic code generation can do for you. You will have to create your table model by writing your own code - handing exceptions for non-numeric input etc. The line of code in your last post is exactly the right place to start.
NetBeans GUI code generation only works for fixed layouts, it's really just "training wheels".

Member Avatar
pspwxp fan
Junior Poster
135 posts since Feb 2009
Reputation Points: 1 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you JamesCherril :)
I was finally able to solve it with your guidance. I added the above mentioned line along with some modifications to code in the constructor method of the form, complete in a try..catch. It is working like a charm, and changing its row count to whatever number is in the text field. It sure is satisfactory to see something finally work

Question Answered as of 9 Months Ago by JamesCherrill
Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

Excellent. Well done. Thanks for the feedback.
J

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article