Как я могу очистить модель JTREE? (Удаление всех узлов)
Вопрос
Так что это метод мой, который называется каждый раз, когда добавляется новый узел. Мне нужна модель, очищенная каждый раз. По умолчанию имеет метод defaulttleistmodel ().
public void fillUserList(){
List<User> userFriends = ClientController.getInstance().getPrieteniiUserului(user);
for(int i=0;i<userFriends.size();i++){
User user = userFriends.get(i);
model.insertNodeInto(new DefaultMutableTreeNode(user.getNume()), root, i);
}
System.out.println(userFriends);
}
. Решение
Я разработал это. Новый код выглядит так.
public void fillUserList(){
List<User> userFriends = ClientController.getInstance().getPrieteniiUserului(user);
root.removeAllChildren(); //this removes all nodes
model.reload(); //this notifies the listeners and changes the GUI
for(int i=0;i<userFriends.size();i++){
User user = userFriends.get(i);
model.insertNodeInto(new DefaultMutableTreeNode(user.getNume()), root, i);
}
}
. Другие советы
Если вам действительно нужно Удалить все узлы , включая root узел, вы должны сделать модель NULL.Как это:
mytree.setModel(null)
. Не связан с StackOverflow