我正在开发一个从数据库中读取并填充表格的应用程序(每行都有一个标题、作者、出版商等)。我有一组文本字段,其中包含每行的额外信息(位置,副本,价格)。现在,当我选择一行并更改任何字段时,更改将完美地反映出来。但是,当我选择多行,并且只更改一个文件(例如价格)而不是仅引用该字段时,文本字段上的所有信息都存储在所有选定的行中。https://i.stack.imgur.com/o5YFi.png
总之,我的问题是如何确定用户实际更改了哪个字段?(所以我只能修改对象的该属性,而不会覆盖前面的属性)。
这就是我从表中获取信息以更新数据库中的书籍的方式
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);`
对于我称之为"创建记录"的每本书,它在其中创建书籍记录并从文本字段中获取信息,这就是为什么 recored 中的 previos 信息被覆盖的原因。
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());
我想要一种方法,以防用户选择多行,则仅更改用户已更改的字段。
定义一个 Map,其中 String 键是子字段的名称,布尔值是字段的更改状态标志。
向每个子字段添加一个 DocumentListener,并在任何更改(插入/删除)时将映射中的字段标志设置为 true。
要应用更改的字段,请仅从映射中获取所有true
标志,并且仅使用子字段的值来更新表数据。
更新:甚至更容易将更改的字段名称保留在集合中。