List of usage examples for javax.swing.tree TreePath TreePath
public TreePath(Object lastPathComponent)
From source file:Main.java
public TestPane() { setLayout(new BorderLayout()); tree = new JTree(); File rootFile = new File("."); DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootFile); model = new DefaultTreeModel(root); tree.setModel(model);//from w w w. ja va2 s .co m tree.setRootVisible(true); tree.setShowsRootHandles(true); add(new JScrollPane(tree)); JButton load = new JButton("Load"); add(load, BorderLayout.SOUTH); load.addActionListener(e -> { DefaultMutableTreeNode r = (DefaultMutableTreeNode) model.getRoot(); root.removeAllChildren(); model.reload(); File f = (File) r.getUserObject(); addFiles(f, model, r); tree.expandPath(new TreePath(r)); }); }
From source file:Main.java
@Override public void valueChanged(TreeSelectionEvent event) { tree.expandPath(new TreePath(e11.getPath())); currentSelectionField.setText(event.getPath().toString()); }
From source file:Main.java
public Main() { DefaultMutableTreeNode forums = new DefaultMutableTreeNode("B"); forums.add(new DefaultMutableTreeNode("T")); DefaultMutableTreeNode articles = new DefaultMutableTreeNode("A"); articles.add(new DefaultMutableTreeNode("A")); DefaultMutableTreeNode examples = new DefaultMutableTreeNode("E"); examples.add(new DefaultMutableTreeNode("E")); rootNode.add(forums);/*from w w w . j a va 2s . c o m*/ rootNode.add(articles); rootNode.add(examples); m_tree.setEditable(true); m_tree.setSelectionRow(0); JScrollPane scrollPane = new JScrollPane(m_tree); getContentPane().add(scrollPane, BorderLayout.CENTER); JPanel panel = new JPanel(); m_addButton = new JButton("Add Node"); m_addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent(); if (selNode == null) { return; } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New"); m_model.insertNodeInto(newNode, selNode, selNode.getChildCount()); TreeNode[] nodes = m_model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); m_tree.scrollPathToVisible(path); m_tree.setSelectionPath(path); m_tree.startEditingAtPath(path); } }); panel.add(m_addButton); getContentPane().add(panel, BorderLayout.SOUTH); m_delButton = new JButton("Delete Node"); m_delButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent(); if (selNode == null) { return; } MutableTreeNode parent = (MutableTreeNode) (selNode.getParent()); if (parent == null) { return; } MutableTreeNode toBeSelNode = (MutableTreeNode) selNode.getPreviousSibling(); if (toBeSelNode == null) { toBeSelNode = (MutableTreeNode) selNode.getNextSibling(); } if (toBeSelNode == null) { toBeSelNode = parent; } TreeNode[] nodes = m_model.getPathToRoot(toBeSelNode); TreePath path = new TreePath(nodes); m_tree.scrollPathToVisible(path); m_tree.setSelectionPath(path); m_model.removeNodeFromParent(selNode); } }); panel.add(m_delButton); getContentPane().add(panel, BorderLayout.SOUTH); setSize(300, 400); setVisible(true); }
From source file:org.robotframework.swing.tree.TreePathWaitable.java
private TreePath buildTreePath(String[] nodeNames) { Object root = getRoot();/* w w w . ja v a2s . c o m*/ TreePath treePathToNode = new TreePath(root); Iterator<Object> currentLevelChildren = getChildren(root); for (String nodeName : nodeNames) { boolean foundMatch = false; while (currentLevelChildren.hasNext()) { Object currentNode = currentLevelChildren.next(); if (nodeTextEquals(nodeName, currentNode)) { currentLevelChildren = getChildren(currentNode); treePathToNode = treePathToNode.pathByAddingChild(currentNode); foundMatch = true; break; } } if (!foundMatch) return null; } return treePathToNode; }
From source file:de.codesourcery.eve.skills.ui.model.DefaultTreeNode.java
@Override public TreePath getPathToRoot() { final List<ITreeNode> stack = new ArrayList<ITreeNode>(); ITreeNode current = this; do {/* w w w . j av a2 s . co m*/ stack.add(current); current = (ITreeNode) current.getParent(); } while (current != null); Collections.reverse(stack); return new TreePath(stack.toArray(new ITreeNode[stack.size()])); }
From source file:EditableTree.java
public EditableTree() { DefaultMutableTreeNode forums = new DefaultMutableTreeNode("A"); forums.add(new DefaultMutableTreeNode("B")); DefaultMutableTreeNode articles = new DefaultMutableTreeNode("E"); articles.add(new DefaultMutableTreeNode("F")); DefaultMutableTreeNode examples = new DefaultMutableTreeNode("G"); examples.add(new DefaultMutableTreeNode("H")); m_rootNode.add(forums);// w ww.j a v a 2s .c o m m_rootNode.add(articles); m_rootNode.add(examples); m_tree.setEditable(true); m_tree.setSelectionRow(0); JScrollPane scrollPane = new JScrollPane(m_tree); getContentPane().add(scrollPane, BorderLayout.CENTER); JPanel panel = new JPanel(); m_addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent(); if (selNode == null) { return; } DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New Node"); m_model.insertNodeInto(newNode, selNode, selNode.getChildCount()); TreeNode[] nodes = m_model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); m_tree.scrollPathToVisible(path); m_tree.setSelectionPath(path); m_tree.startEditingAtPath(path); } }); panel.add(m_addButton); m_delButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) m_tree.getLastSelectedPathComponent(); removeNode(selNode); } }); panel.add(m_delButton); JPanel searchPanel = new JPanel(); searchPanel.setBorder(BorderFactory.createEtchedBorder()); m_searchText = new JTextField(10); searchPanel.add(m_searchText); m_searchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode node = searchNode(m_searchText.getText()); if (node != null) { TreeNode[] nodes = m_model.getPathToRoot(node); TreePath path = new TreePath(nodes); m_tree.scrollPathToVisible(path); m_tree.setSelectionPath(path); } else { System.out.println("Node with string " + m_searchText.getText() + " not found"); } } }); searchPanel.add(m_searchButton); m_searchAndDeleteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DefaultMutableTreeNode node = searchNode(m_searchText.getText()); if (node != null) { removeNode(node); } else { System.out.println("Node with string " + m_searchText.getText() + " not found"); } } }); searchPanel.add(m_searchAndDeleteButton); panel.add(searchPanel); getContentPane().add(panel, BorderLayout.SOUTH); setSize(700, 400); setVisible(true); }
From source file:Tree1.java
public Tree1() { super("Sample Tree [OID]"); setSize(400, 300);//from ww w. j a va 2 s. c o m Object[] nodes = new Object[5]; DefaultMutableTreeNode top = new DefaultMutableTreeNode(new OidNode(1, "ISO")); DefaultMutableTreeNode parent = top; nodes[0] = top; DefaultMutableTreeNode node = new DefaultMutableTreeNode(new OidNode(0, "standard")); parent.add(node); node = new DefaultMutableTreeNode(new OidNode(2, "member-body")); parent.add(node); node = new DefaultMutableTreeNode(new OidNode(3, "org")); parent.add(node); parent = node; nodes[1] = parent; node = new DefaultMutableTreeNode(new OidNode(6, "dod")); parent.add(node); parent = node; nodes[2] = parent; node = new DefaultMutableTreeNode(new OidNode(1, "internet")); parent.add(node); parent = node; nodes[3] = parent; node = new DefaultMutableTreeNode(new OidNode(1, "directory")); parent.add(node); node = new DefaultMutableTreeNode(new OidNode(2, "mgmt")); parent.add(node); nodes[4] = node; node.add(new DefaultMutableTreeNode(new OidNode(1, "mib-2"))); node = new DefaultMutableTreeNode(new OidNode(3, "experimental")); parent.add(node); node = new DefaultMutableTreeNode(new OidNode(4, "private")); node.add(new DefaultMutableTreeNode(new OidNode(1, "enterprises"))); parent.add(node); node = new DefaultMutableTreeNode(new OidNode(5, "security")); parent.add(node); node = new DefaultMutableTreeNode(new OidNode(6, "snmpV2")); parent.add(node); node = new DefaultMutableTreeNode(new OidNode(7, "mail")); parent.add(node); m_model = new DefaultTreeModel(top); m_tree = new JTree(m_model); DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); renderer.setOpenIcon(new ImageIcon("opened.gif")); renderer.setClosedIcon(new ImageIcon("closed.gif")); renderer.setLeafIcon(new ImageIcon("leaf.gif")); m_tree.setCellRenderer(renderer); m_tree.setShowsRootHandles(true); m_tree.setEditable(false); TreePath path = new TreePath(nodes); m_tree.setSelectionPath(path); m_tree.addTreeSelectionListener(new OidSelectionListener()); JScrollPane s = new JScrollPane(); s.getViewport().add(m_tree); getContentPane().add(s, BorderLayout.CENTER); m_display = new JTextField(); m_display.setEditable(false); getContentPane().add(m_display, BorderLayout.SOUTH); WindowListener wndCloser = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; addWindowListener(wndCloser); setVisible(true); }
From source file:TreeIt.java
public TreeIt() { JFrame f = new JFrame(); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Calendar"); DefaultMutableTreeNode months = new DefaultMutableTreeNode("Months"); root.add(months);//from w ww .j a v a2 s. c om 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:edu.mbl.jif.datasetconvert.CheckboxTreeDimensions.java
/** * Initialize the tree./*from ww w. ja v a 2s. co m*/ * */ private JScrollPane getCheckboxTree() { if (this.checkboxTree == null) { this.checkboxTree = new CheckboxTree(getDimensionsTreeModel(sumMD)); //this.checkboxTree.addKeyListener(new RefreshListener()); System.out.println(this.checkboxTree.toString()); this.checkboxTree.getCheckingModel().setCheckingMode(TreeCheckingModel.CheckingMode.PROPAGATE); this.checkboxTree.setRootVisible(true); this.checkboxTree.setEnabled(true); this.checkboxTree.expandAll(); DefaultMutableTreeNode mn = (DefaultMutableTreeNode) this.checkboxTree.getModel().getRoot(); // mn = (DefaultMutableTreeNode) mn.getChildAt(2); // mn = (DefaultMutableTreeNode) mn.getChildAt(2); // System.out.println("row number: " + this.checkboxTree.getRowForPath(new TreePath(mn.getPath()))); this.checkboxTree.addCheckingPath(new TreePath(mn.getPath())); this.checkboxTree.addTreeCheckingListener(new TreeCheckingListener() { public void valueChanged(TreeCheckingEvent e) { System.out.println("checking set changed, leading path: " + ((TreeNode) e.getPath().getLastPathComponent()).toString()); System.out.println("checking roots: "); TreePath[] cr = CheckboxTreeDimensions.this.checkboxTree.getCheckingRoots(); for (TreePath path : cr) { System.out.println(path.getLastPathComponent()); } System.out.println("\nPaths: "); TreePath[] cp = CheckboxTreeDimensions.this.checkboxTree.getCheckingPaths(); for (TreePath path : cp) { System.out.println(path.toString()); } } }); } return new JScrollPane(this.checkboxTree); }
From source file:jshm.gui.GuiUtil.java
/** * Expands all nodes of the supplied tree starting at startDepth. * This should only be used when there is a known maximum depth. * @param tree/* w w w . ja va 2 s. co m*/ * @param startDepth */ public static void expandTreeFromDepth(JXTreeTable tree, int startDepth) { TreeTableModel model = tree.getTreeTableModel(); Object root = model.getRoot(); tree.expandAll(); // expandTreeFromDepth(tree, model, root, null, 0, startDepth); collapseTreeToDepth(tree, model, root, new TreePath(root), 0, startDepth); }