wood burning stoves 2.0*
The moose likes Java in General and the fly likes adding a blank row to a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "adding a blank row to a JTable" Watch "adding a blank row to a JTable" New topic
Author

adding a blank row to a JTable

John Ellis
Greenhorn

Joined: Dec 27, 2002
Posts: 2
Hi,
I'm new here. Let me know if a different style of posting works better.
I would like to add a blank row to the bottom of a JTable so the user can enter new data into the blank row of the table. This data will then be read and used to update the table model and the database. I do not want to use a pop-up input frame. The table is generated through an SQL statement sent to a postgresql database. The resultset that is produced by the query is used as the data for the table model. My create statement in by table model is:
Statement st = db.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultsSet.CONCUR_UPDATABLE);
I have tried extending both the DefaultTableModel and the AbstractTableModel. The DefaultTableModel has an addRow method, but when I use it as follows, it gives an arrayIndexOutOfBoundsException. I am not sure how the data is represented in the tableModel - Vectors or Object[].
tableMod = new TheTableModel(db,query);
Vector row = new Vector();
tableMod.addRow(row);
The table displays correctly when I drop the Vector and addRow lines above, but of course, I don't get the blank row.
Thanks in advance.
John
[ December 27, 2002: Message edited by: John Ellis ]
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Originally posted by John Ellis:
I'm new here. Let me know if a different style of posting works better.

Welcome to JavaRanch.
This posting style is fine. You gave useful information (too many people just briefly give their problem, giving details helps, and makes your post more likely to be answered).

Originally posted by John Ellis:
I would like to add a blank row to the bottom of a JTable so the user can enter new data into the blank row of the table. This data will then be read and used to update the table model and the database. I do not want to use a pop-up input frame. The table is generated through an SQL statement sent to a postgresql database. The resultset that is produced by the query is used as the data for the table model. My create statement in by table model is:
Statement st = db.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultsSet.CONCUR_UPDATABLE);
I have tried extending both the DefaultTableModel and the AbstractTableModel. The DefaultTableModel has an addRow method, but when I use it as follows, it gives an arrayIndexOutOfBoundsException. I am not sure how the data is represented in the tableModel - Vectors or Object[].
tableMod = new TheTableModel(db,query);
Vector row = new Vector();
tableMod.addRow(row);
The table displays correctly when I drop the Vector and addRow lines above, but of course, I don't get the blank row.

Well, consider creating your own model. Simply extend the AbstractTableModel and use a Vector as the underlying dataset (what will be appropriate depends on your needs: how often you add, remove, insert, sort, access, etc). You'll probably also want to create a container class to hold the entire row set.
If you're really motivated, you can get the Java source code and look at how the DefaultTableModel is implamented.
--Mark
John Ellis
Greenhorn

Joined: Dec 27, 2002
Posts: 2
Thanks Mark,
I tried extending both the DefaultTableModel and the AbstractTableModel. The default model has addRow() as a member, but it did not work for me. I am not sure how the data from a resultset returned by SQL is stored. Is it a Vector or an Object[] or something else?
I tried writing my own addRow(), but failed.
I also tried using the resultset methods, but I think postgresql does not implement them all.
I am beginning to think I will be forces to use an input frame and read the data from it, store it in the database and then refresh the table. It should be possible to edit the tableModel directly and then update the database after.
John
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: adding a blank row to a JTable
 
Similar Threads
JTable(!!!)
Dynamic JTable
Updating JTable data sources
Updating JTable data sources
Columns on JTable