• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

adding a blank row to a JTable

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Author
Posts: 6055
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
It's a tiny ad only because the water is so cold.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic