List of usage examples for javax.swing.tree DefaultMutableTreeNode getPath
public TreeNode[] getPath()
From source file:Main.java
public static void expandTreeNode(JTree tree, DefaultMutableTreeNode node, int depth) { if (depth <= 0) { return;/*w ww .ja v a 2s . c o m*/ } tree.expandPath(new TreePath(node.getPath())); for (int i = 0; i < node.getChildCount(); i++) { expandTreeNode(tree, (DefaultMutableTreeNode) node.getChildAt(i), depth - 1); } }
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;/*from w ww . j a va2 s . co 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 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);//from w ww. ja v a 2 s . c om } }
From source file:Main.java
public static void expand(JTree tree, int level) { DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) tree.getModel().getRoot(); while (currentNode != null) { if (currentNode.getLevel() <= level) { tree.expandPath(new TreePath(currentNode.getPath())); }/*from ww w.j av a 2 s . c o m*/ currentNode = currentNode.getNextNode(); } }
From source file:TreeIt.java
public TreeIt() { JFrame f = new JFrame(); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Calendar"); DefaultMutableTreeNode months = new DefaultMutableTreeNode("Months"); root.add(months);// w ww . j a va 2 s .c o m String monthLabels[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; for (int i = 0, n = monthLabels.length; i < n; i++) months.add(new DefaultMutableTreeNode(monthLabels[i])); DefaultMutableTreeNode weeks = new DefaultMutableTreeNode("Weeks"); root.add(weeks); String weekLabels[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; for (int i = 0, n = weekLabels.length; i < n; i++) weeks.add(new DefaultMutableTreeNode(weekLabels[i])); JTree jt = new JTree(root); jt.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { TreePath path = e.getPath(); System.out.println("Picked: " + path.getLastPathComponent()); Object elements[] = path.getPath(); for (int i = 0, n = elements.length; i < n; i++) { System.out.print("->" + elements[i]); } System.out.println(); } }); DefaultMutableTreeNode lastLeaf = root.getLastLeaf(); TreePath path = new TreePath(lastLeaf.getPath()); jt.setSelectionPath(path); jt.setCellRenderer(new MyCellRenderer()); JScrollPane jsp = new JScrollPane(jt); Container c = f.getContentPane(); c.add(jsp, BorderLayout.CENTER); f.setSize(250, 250); f.show(); }
From source file:Main.java
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); if (parent == null) { parent = rootNode;//from w w w .j av a 2 s . c o m } treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); if (shouldBeVisible) { tree.scrollPathToVisible(new TreePath(childNode.getPath())); } return childNode; }
From source file:com.mindcognition.mindraider.ui.swing.explorer.LabelsTree.java
public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) labelsTree.getLastSelectedPathComponent(); if (node != null) { logger.debug("Labels tree selection path: " + node.getPath()[node.getLevel()]); if (node.getUserObject() instanceof NotebookNodeUserObject) { NotebookNodeUserObject labelNode = (NotebookNodeUserObject) node.getUserObject(); // TODO bundle StatusBar.show("Label " + labelNode.getNotebookUri() + " selected..."); notebooksTree.reloadModel(labelNode.getNotebookUri()); }/* ww w . j ava2s .c o m*/ } }
From source file:com.mindcognition.mindraider.ui.swing.explorer.NotebooksTree.java
public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) notebooksTree.getLastSelectedPathComponent(); if (node != null && !notebooksTree.isRoot(node)) { logger.debug("Outlines tree selection path: " + node.getPath()[node.getLevel()]); if (node.getUserObject() instanceof NotebookNodeUserObject) { NotebookNodeUserObject notebookNode = (NotebookNodeUserObject) node.getUserObject(); // TODO bundle StatusBar.show("Outline " + notebookNode.getNotebookUri() + " selected..."); try { OutlineJPanel.getInstance().conceptJPanel.clear(); MindRaider.profile.setActiveOutlineUri(null); MindRaider.outlineCustodian.loadOutline(new URI(notebookNode.getNotebookUri())); OutlineJPanel.getInstance().refresh(); } catch (URISyntaxException e1) { // TODO option with question whether to remove non-existent notebook logger.debug("Unable to load Outline: ", e1); }/*from w w w . j av a 2s . c o m*/ } if (notebooksTree.getToolbar() != null) { notebooksTree.getToolbar().enableDisableToolbarButtons(NotebooksTree.LEVEL_NOTEBOOKS); } } else { if (notebooksTree.getToolbar() != null) { notebooksTree.getToolbar().enableDisableToolbarButtons(NotebooksTree.LEVEL_ROOT); } } }
From source file:uk.co.markfrimston.tasktree.Main.java
protected DefaultMutableTreeNode addTask(DefaultMutableTreeNode parent, int childPos, String name, boolean show) { DefaultMutableTreeNode newNode = taskTree.addTask(parent, childPos, name); if (show) {//from w w w . j a v a 2 s .c o m tree.makeVisible(new TreePath(newNode.getPath())); } return newNode; }
From source file:appletComponentArch.DynamicTree.java
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child, boolean shouldBeVisible) { DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child); if (parent == null) { parent = rootNode;//from w ww. ja v a 2 s . c o m } //It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode treeModel.insertNodeInto(childNode, parent, parent.getChildCount()); //Make sure the user can see the lovely new node. if (shouldBeVisible) { tree.scrollPathToVisible(new TreePath(childNode.getPath())); } return childNode; }