Domanda

I have a Jlist with a DefaultListModel with data from XML.

I want to be able change the name of the item in the Jlist. but the DefaultListModel has no update method.

So if the user clicks on a name it should edit the name.

So far I thought if I get the location of the item and remove it and update with new data. 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 ?

My code :

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
            }

    }
È stato utile?

Soluzione

"I want to be able change the name of the item in the Jlist. but the DefaultListModel has no update method."

What makes you say that? Have you looked carefully at the docs ?

What do you think this method does?

OR

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top