| Author |
Can I set up a JTable columns?
|
ben riches
Ranch Hand
Joined: Nov 08, 2002
Posts: 126
|
|
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
Joined: May 29, 2003
Posts: 1258
|
|
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.
|
Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
|
 |
ben riches
Ranch Hand
Joined: Nov 08, 2002
Posts: 126
|
|
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
Joined: Jun 08, 2002
Posts: 3178
|
|
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?
|
Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
|
 |
ben riches
Ranch Hand
Joined: Nov 08, 2002
Posts: 126
|
|
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
Joined: Jun 08, 2002
Posts: 3178
|
|
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
Joined: Nov 08, 2002
Posts: 126
|
|
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
Joined: Jun 08, 2002
Posts: 3178
|
|
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
Joined: Nov 08, 2002
Posts: 126
|
|
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
Joined: Jun 08, 2002
Posts: 3178
|
|
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....
|
 |
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
|
|
|
Moved to the Swing forum.
|
"JavaRanch, where the deer and the Certified play" - David O'Meara
|
 |
Vishal Methi
Ranch Hand
Joined: Jan 14, 2004
Posts: 52
|
|
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.............
|
 |
 |
|
|
subject: Can I set up a JTable columns?
|
|
|