Java examples for Swing:JTree
deselect All JTree
//package com.java2s; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.swing.JTree; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; public class Main { public static void deselectAll(JTree tree) { if (tree == null) { throw new NullPointerException("tree == null"); }// w w w . j a va 2 s.c o m TreeSelectionModel m = tree.getSelectionModel(); if (m != null) { m.clearSelection(); } } /** * Deselektiert alle Treeitems von mehreren Trees. * * @param trees Trees * @return Previous selected paths of trees with selected items */ public static Map<JTree, List<TreePath>> clearSelection( List<JTree> trees) { if (trees == null) { throw new NullPointerException("trees == null"); } Map<JTree, List<TreePath>> selectionPaths = new HashMap<>(); for (JTree tree : trees) { if (tree.getSelectionCount() > 0) { TreePath[] paths = tree.getSelectionPaths(); if (paths != null && paths.length > 0) { // should not be necessary, "safety belt" selectionPaths.put(tree, Arrays.asList(paths)); } tree.clearSelection(); } } return selectionPaths; } }