Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I set up a JTable columns?

 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
How can I set up my JTable columns to have the amount the user specifies?
for example when the user types in 50 in JTextField 1 I want the JTables columns to then set to 50.
How can this be done?
Thanks
ben
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not going to wade through all your code (too lazy), so I'll just assume it all works.
Basically, if I understand you correctly, you want to create N columns when the user inputs N into some JTextField. Basically, you have implemented everything except for that behavior in your code.
Instead of using
String[] columnNames = {"First Name", "Last Name"};
you will want to
String[] columnNames = new String[N];
for (int i = 0; i < columnNames; i++) {
columnNames = new String(new Integer(i).toString());
// or however you want them named.
}
// set JTable columns to columnNames just like before, or using the API
I hope this helps a bit. I'm sure if you protest, somebody out there will post you some real code.
 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if I sound silly.
I am still having trouble doing this.
I want to be able to set the JTable rows going down to have as many as the user wants ie if the user types in 50, then there will be 50 rows going down.
I hope you understand more now.
Thanks
Ben
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ben riches:
Sorry if I sound silly.
I am still having trouble doing this.
I want to be able to set the JTable rows going down to have as many as the user wants ie if the user types in 50, then there will be 50 rows going down.
I hope you understand more now.
Thanks
Ben

For the columns, I believe that it's not a big deal... But for the rows, I'm not clear with your specification...
Do u want to set the table with blank 50 rows, when the user types 50 in the textfield? Actually, the number of rows depends on the result that u fetched from the database... The number of records that u get from querying the database is representing the number of rows appeared in your JTable... I'm not sure about the number of rows specified by the user? Could u please mention more about ur specification?
 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ko Ko,
Yes I do want the user to type in a text field how many rows she wants, then after she has typed how many rows she wants I want the JTable to have that amount of rows.
What I want to do eventully is read in a file count how many lines are in that file and then change the rows to howmany lines are in the file.
Thanks
Ben
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may use
yourTable.getModel().setDataVector(vectorOfRecords, vectorOfColumnNames) method to set the data into ur table...
You do have to get the data from the file that u want and make them into records and put them into the vectorOfRecords... After that, sent as a parameter to the setDataVector method of your table model...
If I am not wrong, u cannot set the rows before u get the records to be inserted in the table...
Hope this helps...
 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but can it work with an int that holds lets say the number 50, the int has already counted the number of lines there is in the file.
would this work?
Thanks
Ben
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't mean that, ben...
What are u going to do with the 50 blank rows? Just to show that there are 50 blank rows fixed in the table or sthing?
The problem is that I don't not clear with the specification u want...
 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Basically I want to show that the user has 50 blank rows in the JTable,
Then they can type in the rows what data they need.
Thanks
Ben
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ben riches:
Yes Basically I want to show that the user has 50 blank rows in the JTable,
Then they can type in the rows what data they need.
Thanks
Ben

Why don't u use the following constructor to build ur table? U can have a look at the Java API for the usage... This will surely create the empty cells as much as you want... It is using DefaultTableModel....
Hope it helps....

JTable(int numRows, int numColumns)
Constructs a JTable with numRows and numColumns of empty cells using DefaultTableModel.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the Swing forum.
 
Vishal Methi
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Firstly you need create an object of DefaultTableModel and then set this table model in JTable like jTable1.setModel(defaultTableModel) and my using the object of defaultTableModel u can insert row dinamically like
defaultTableModel.setRowCount(int noofRows); may be this will some helpful for u . is this the answer u wish.............
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic