• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How add/delete rows via JTable?

 
Ranch Hand
Posts: 143
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22863
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a DefaultTableModel as your JTable's model, then use its methods to add and remove rows.
 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic