Java JTree getSelectionFromTree(JTree tree)

Here you can find the source of getSelectionFromTree(JTree tree)

Description

get Selection From Tree

License

Open Source License

Declaration

public static ArrayList<String> getSelectionFromTree(JTree tree) 

Method Source Code

//package com.java2s;
/*/* w  w  w .j  a va  2  s . com*/
 * 
This file is part of YaBS.
    
YaBS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
YaBS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
    
 * 
 */

import java.util.ArrayList;

import java.util.Iterator;

import javax.swing.JTree;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.TreePath;

public class Main {
    public static ArrayList<String> getSelectionFromTree(JTree tree) {
        ArrayList<String> list = new ArrayList<String>();
        TreePath[] nodes = tree.getSelectionPaths();

        for (int i = 0; i < nodes.length; i++) {
            TreePath temp = nodes[i];
            Object tempObj = temp.getLastPathComponent();
            DefaultMutableTreeNode treNode = (DefaultMutableTreeNode) tempObj;
            String device = String.valueOf(treNode.getUserObject());
            list.add(device);
        }

        return list;
    }

    /**
     * list 1 + 2 must have same element count!
     *
     * @param list1
     * @param list2
     * @return list1.values(n) + list2.values(n)
     * @throws Exception
     */
    public static ArrayList<Double> add(ArrayList<Double> list1, ArrayList<Double> list2) throws Exception {

        Iterator it1 = list1.iterator();
        Iterator it2 = list2.iterator();

        if (list1.size() != list2.size()) {
            throw new Exception("list 1 + 2 must have same element count!");
        }
        @SuppressWarnings("unchecked")
        ArrayList<Double> list3 = new ArrayList();

        while (it1.hasNext() && it2.hasNext()) {
            Double value = (Double) it1.next() + (Double) it2.next();
            list3.add(value);
        }

        return list3;
    }
}

Related

  1. findByName(JTree tree, String[] names)
  2. findTree(Container container)
  3. getCurrentWidth(JTree tree, int row)
  4. getExpansionState(JTree tree, int row)
  5. getProjectTree()
  6. getStopRow(JTree tree, int startRow)
  7. getTreeImage(String imageName)
  8. getTreeObject(JTree tree, Class type)
  9. getTreeSelectionBorderColor()