void () { final FileTemplate selectedTemplate = getSelectedTemplate(); if (selectedTemplate == null) { return; } DefaultListModel<?> model = (DefaultListModel<?>) myList.getModel(); for (FileTemplate child : selectedTemplate.getChildren()) { model.removeElement(child); } final int selectedIndex = myList.getSelectedIndex(); model.remove(selectedIndex); if (!model.isEmpty()) { myList.setSelectedIndex(Math.min(selectedIndex, model.size() - 1)); } onTemplateSelected(); }