【问题标题】:how to know which JTextField is changed in a set of fields如何知道在一组字段中更改了哪个 JTextField
【发布时间】:2015-01-25 16:48:15
【问题描述】:

我正在开发一个从数据库读取并填充表格的应用程序(每行都有一个标题、作者、出版商等)。我有一组 TextFields,每行都有额外的信息(位置、副本、价格)。现在,当我选择一行并更改任何字段时,更改会完美反映。但是,当我选择多行并仅更改一个字段(例如价格)而不是仅反映该字段时,文本字段上的所有信息都存储在所有选定的行中。这是我的程序的屏幕截图 http://i.imgur.com/YMw7iy5.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);`

对于我调用创建记录的每本书,它会在其中创建书籍记录并从文本字段中获取信息,这就是记录中的 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());

我想要一种方法来仅更改用户已更改的字段,以防他选择了多行。

【问题讨论】:

  • 禁用多选?
  • @MadProgrammer 这无济于事,如果用户想为多行设置相同的值,他将不得不一一进行。选择范围更容易:D

标签: java user-interface netbeans jtextfield


【解决方案1】:

定义一个 Map,其中 String 键是子字段的名称,布尔值是字段的更改状态标志。

为每个子字段添加一个 DocumentListener,并在任何更改(插入/删除)时在地图中将该字段的标志设置为 true。

要应用更改的字段,只需从地图中获取所有 true 标志,并仅使用子字段的值来更新表数据。

更新:甚至更容易将更改的字段名称保留在 Set 中。

【讨论】:

  • 非常感谢,我对 documentListener 有一个想法,但不知道如何应用它。我不知道如何使用集合,我会尝试使用地图。谢谢:) PS:对不起,但我不能投票给你:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 2016-03-05
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
相关资源
最近更新 更多