Clear JTree Selection - Java Swing

Java examples for Swing:JTree

Description

Clear JTree Selection

Demo Code


//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;

public class Main {
    /**//from   w w w. j  a v  a 2  s.com
     * 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;
    }
}

Related Tutorials