• Post Reply Bookmark Topic Watch Topic
  • New Topic

How add/delete rows via JTable?  RSS feed

 
Phil Freihofner
Ranch Hand
Posts: 128
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am starting to suspect I'm stuck in one of those mental traps where one tries to do something via a method that works in another language (VBA, in this case) but is perhaps not the best method in Java.

I have an object that contains a varying number of items. Each item has four properties. I would like to set up a GUI to edit these properties. My first inclination is to use a JTable. The four properties translate to four columns, and the varying number of items can be represented in the JTable's rows.

I've had no problem editing and updating existing data in this fashion. What baffles me is how to implement the deletion or addition of rows (corresponding to the possible change in the number of items collected in the object).

I can set up a button to append or delete, and programmatically alter the data array used in the table's model, but after such an operation, I can't get the JTable to display the new number of records. Is it possible to alter the model's dimensions (specifically, the number of rows) and redisplay a JTable? Or do I have to rebuild the JTable from scratch with every row count change, and .remove the previous and .add the new to the display?

In Access/VBA, it is a pretty simple matter to append or delete a "record" on a form set up to display as a table. Any suggestions as to how to go about this via Swing tools?
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a DefaultTableModel as your JTable's model, then use its methods to add and remove rows.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!