【问题标题】:How to update the string name of an item inside a DefaultListModel [duplicate]如何更新 DefaultListModel 中项目的字符串名称 [重复]
【发布时间】:2014-01-23 15:40:15
【问题描述】:

我有一个 Jlist 和一个 DefaultListModel,其中包含来自 XML 的数据。

我希望能够更改Jlist 中的项目名称。但DefaultListModel 没有更新方法。

所以如果用户点击一个名字,它应该编辑这个名字。

到目前为止,我想如果我获取项目的位置并将其删除并使用新数据进行更新。但是,如果我使用新名称进行更新,它会被放在与旧名称相同的位置还是会搞砸?

我的代码:

private class EditName extends AbstractAction {
        public EditName() {
            putValue(NAME, "Change Name");
            putValue(SHORT_DESCRIPTION, "Some short description");
        }

        public void actionPerformed(ActionEvent e) {

             int x = objTypeJList.getSelectedIndex();
               String newName = JOptionPane.showInputDialog("New Name?");

                 if (x >= 0) {
                   String oldName = ReadXMLFile.getInstance().getModel().getElementAt(x).toString();
                   ReadXMLFile.getInstance().getModel().removeElementAt(x);
                   objTypeJList.setModel(ReadXMLFile.getInstance().getModel());
                }
                    // newText I wanna add into the the location I edit
            }

    }

【问题讨论】:

  • But if I update with then new name will it be put in the same location as the old or will things be messed up ? 试试看,让我们知道会发生什么。你通过尝试来学习。为什么你的代码会删除一个元素,然后更新整个模型?

标签: java swing jlist defaultlistmodel


【解决方案1】:

“我希望能够更改 Jlist 中项目的名称。但是 DefaultListModel 没有更新方法。”

是什么让你这么说? docs你仔细看了吗?

你觉得这个方法有什么作用?

【讨论】:

  • 对,没错。大声笑我只阅读方法名称。
猜你喜欢
  • 2015-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
相关资源
最近更新 更多