I am working on a app that reads from a database and fills a Table(Each row has a Title, Author, Publisher. etc) . I have a set of TextFields that has extra information for each row(location, copies, price). now when i select a single row, and change any fields the change is reflected perfectly. However, when i select multiple rows, and changes only one filed(e.g price) instead of refelcting only that field, all the information on the textfields are stored in all the selected rows.this is a screenshot of my program https://i.sstatic.net/o5YFi.png
In summary, my problem is how to determine what field the user actually changed? (so i can modify only that attribute for the object, without overwriting the previous ones).
This is how i get the info from the Table to update the book in DB
ArrayList<Title> books = getAll(rowindex);
int index=0;
for(Title b:books){
b.setIsbn((String) recTable.getValueAt(rowindex[index], 0));
b.setTitle((String) recTable.getValueAt(rowindex[index], 1));
b.setAuthor((String) recTable.getValueAt(rowindex[index], 2));
b.setCountry((String) recTable.getValueAt(rowindex[index], 3));
b.setPub((String) recTable.getValueAt(rowindex[index], 4));
b.setYear((int) recTable.getValueAt(rowindex[index], 5));
b.setEd((String) recTable.getValueAt(rowindex[index], 6));
b.setPrice((Double) recTable.getValueAt(rowindex[index], 7));
b.setCopies((int) recTable.getValueAt(rowindex[index], 8));
Title_Record titlerecord = new Title_Record();
titlerecord.setRecord(createRecord(b,rowindex[index++]));
b.setRecord(titlerecord);
ctrl.updateTitle(b);`
for each book i call create record, where it creates the book record and get the info from text fields, which is why the previos info in the recored gets overwritten.
field = factory.newDataField("960",' ',' ');
// field.setTag("960");
sf1 = factory.newSubfield('a');
sf1.setData(acqType.getText());
sf2 = factory.newSubfield('g');
sf2.setData(format.getText());
sf3 = factory.newSubfield('i');
sf3.setData(ordType.getText());
Subfield sf4 = factory.newSubfield('k');
sf4.setData(rloc.getText());
Subfield sf5 = factory.newSubfield('l');
sf5.setData(bloc.getText());
Subfield sf6 = factory.newSubfield('m');
sf6.setData(status960.getText());
Subfield sf7 = factory.newSubfield('o');
sf7.setData(copies960.getText());
Subfield sf8 = factory.newSubfield('s');
sf8.setData(price960.getText());
Subfield sf9 = factory.newSubfield('t');
sf9.setData(location.getText());
Subfield sf10 = factory.newSubfield('u');
sf10.setData((String) fundcambo.getSelectedItem());
Subfield sf11 = factory.newSubfield('v');
sf11.setData(vendor.getText());
Subfield sf12 = factory.newSubfield('w');
sf12.setData(lang.getText());
Subfield sf13 = factory.newSubfield('x');
sf13.setData(cCode.getText());
i want a way to change only the fields the user has changed in case he selected multiple rows.
Define a Map where the String key is subfield's name and Boolean value is the field's changed state flag.
Add a DocumentListener to each the subfield and on any change (insert/delete) set the flag for the field to true in the map.
To apply changed fields only get all true
flags from the map and use the subfields' values only to update table data.
UPDATE: Or even easier to keep changed fields' names in a Set.