List of usage examples for javax.swing.tree TreePath TreePath
public TreePath(Object lastPathComponent)
From source file:Utils.java
public static TreePath getPath(TreeNode treeNode) { List<Object> nodes = new ArrayList<Object>(); if (treeNode != null) { nodes.add(treeNode);/* w w w . j ava 2s.c om*/ treeNode = treeNode.getParent(); while (treeNode != null) { nodes.add(0, treeNode); treeNode = treeNode.getParent(); } } return nodes.isEmpty() ? null : new TreePath(nodes.toArray()); }
From source file:Main.java
public static void expandTree(JTree tree, boolean expand) { expandAll(tree, new TreePath((TreeNode) tree.getModel().getRoot()), expand); }
From source file:Main.java
public static void expandTree(JTree tree) { DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot(); @SuppressWarnings("unchecked") Enumeration<DefaultMutableTreeNode> e = root.breadthFirstEnumeration(); while (e.hasMoreElements()) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement(); if (node.isLeaf()) continue; int row = tree.getRowForPath(new TreePath(node.getPath())); tree.expandRow(row);/*w ww. j av a 2 s . c o m*/ } }
From source file:Main.java
public void expandAll(JTree tree) { TreeNode root = (TreeNode) tree.getModel().getRoot(); expandAll(tree, new TreePath(root)); }
From source file:Main.java
/** Fully expands the given JTree from the specified node. */ public static void expandTree(final JTree tree, final DefaultMutableTreeNode node) { if (node.isLeaf()) return;/*w w w. ja v a2 s. c o m*/ tree.expandPath(new TreePath(node.getPath())); final Enumeration e = node.children(); while (e.hasMoreElements()) { expandTree(tree, (DefaultMutableTreeNode) e.nextElement()); } }
From source file:Main.java
public TreePath[] getPaths(JTree tree, boolean expanded) { TreeNode root = (TreeNode) tree.getModel().getRoot(); List<TreePath> list = new ArrayList<TreePath>(); getPaths(tree, new TreePath(root), expanded, list); return (TreePath[]) list.toArray(new TreePath[list.size()]); }
From source file:Main.java
public static Enumeration<TreePath> saveExpansionState(JTree tree) { return tree.getExpandedDescendants(new TreePath(tree.getModel().getRoot())); }
From source file:Main.java
public TreePath getPath(TreeNode node) { List<TreeNode> list = new ArrayList<TreeNode>(); while (node != null) { list.add(node);//w w w . j a v a2s . c om node = node.getParent(); } Collections.reverse(list); return new TreePath(list.toArray()); }
From source file:Main.java
public Main() { setLayout(new BorderLayout()); treeModel = new DefaultTreeModel(root); tree = new JTree(treeModel); treeScroll = new JScrollPane(tree); add(treeScroll, BorderLayout.WEST); properties.setProperty("foo1", "bar1"); properties.setProperty("foo2", "bar2"); properties.setProperty("foo3", "bar3"); properties.setProperty("foo4", "bar4"); Set<Object> keySet = properties.keySet(); for (Object key : keySet) { root.add(new DefaultMutableTreeNode(key)); }/*from w ww. j av a2s .co m*/ tree.expandPath(new TreePath(root)); descriptionLabel = new JLabel(NOTHING_SELECTED); add(descriptionLabel, BorderLayout.CENTER); tree.addTreeSelectionListener(e -> { DefaultMutableTreeNode selection = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (selection != null) { String key = (String) selection.getUserObject(); String command = properties.getProperty(key); descriptionLabel.setText(command); } else { descriptionLabel.setText(NOTHING_SELECTED); } }); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); }
From source file:Main.java
public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TreeNode root = getNodes();//from w w w . j av a2 s . com DefaultTreeModel model = new DefaultTreeModel(root); JTree tree = new JTree(model); tree.setRootVisible(false); JButton add = new JButton("add new"); add.addActionListener(e -> { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (selectedNode == null) { return; } MyObject obj = (MyObject) selectedNode.getUserObject(); MyObject newChild = new MyObject(obj.name + "-" + (obj.childs.size() + 1)); obj.childs.add(newChild); DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(newChild); model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount()); TreeNode[] nodes = model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); }); JButton print = new JButton("print childs"); print.addActionListener(e -> { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (selectedNode == null) { return; } MyObject obj = (MyObject) selectedNode.getUserObject(); System.out.println(obj.childs); }); JPanel btns = new JPanel(); btns.add(add); btns.add(print); add(new JScrollPane(tree)); add(btns, BorderLayout.SOUTH); pack(); setVisible(true); }