1.11M Members

Set JTable rowCount

 
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?

 
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.

 
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.

 
0
 

Just make your model the right size.

 
0
 

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

 
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

 
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.

 
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".

 
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 1 Year Ago by JamesCherrill
 
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