【发布时间】: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