List of usage examples for javax.swing.event TreeModelEvent getChildren
public Object[] getChildren()
getPath
at the locations specified by getChildIndices
. From source file:Main.java
public static void main(String args[]) { JFrame f = new JFrame("JTree Demo"); f.setSize(260, 240);/*w ww . ja va2 s .co m*/ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); DefaultMutableTreeNode aNode = new DefaultMutableTreeNode("A"); root.add(aNode); DefaultMutableTreeNode bNode = new DefaultMutableTreeNode("B"); aNode.add(bNode); DefaultMutableTreeNode cNode = new DefaultMutableTreeNode("C"); aNode.add(cNode); cNode.add(new DefaultMutableTreeNode("D")); cNode.add(new DefaultMutableTreeNode("E")); DefaultMutableTreeNode fNode = new DefaultMutableTreeNode("F"); root.add(fNode); DefaultMutableTreeNode gNode = new DefaultMutableTreeNode("G"); fNode.add(gNode); fNode.add(new DefaultMutableTreeNode("H")); gNode.add(new DefaultMutableTreeNode("I")); JTree jtree = new JTree(root); jtree.setEditable(true); TreeSelectionModel tsm = jtree.getSelectionModel(); tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); jtree.addTreeExpansionListener(new TreeExpansionListener() { public void treeExpanded(TreeExpansionEvent tee) { TreePath tp = tee.getPath(); System.out.println("Expansion: " + tp.getLastPathComponent()); } public void treeCollapsed(TreeExpansionEvent tee) { TreePath tp = tee.getPath(); System.out.println("Collapse: " + tp.getLastPathComponent()); } }); jtree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent tse) { TreePath tp = tse.getPath(); System.out.println("Selection event: " + tp.getLastPathComponent()); } }); jtree.getModel().addTreeModelListener(new TreeModelListener() { public void treeNodesChanged(TreeModelEvent tme) { TreePath tp = tme.getTreePath(); Object[] children = tme.getChildren(); DefaultMutableTreeNode changedNode; if (children != null) changedNode = (DefaultMutableTreeNode) children[0]; else changedNode = (DefaultMutableTreeNode) tp.getLastPathComponent(); System.out.println("Model change path: " + tp + "New data: " + changedNode.getUserObject()); } public void treeNodesInserted(TreeModelEvent tme) { } public void treeNodesRemoved(TreeModelEvent tme) { } public void treeStructureChanged(TreeModelEvent tme) { } }); f.add(new JScrollPane(jtree)); f.setVisible(true); }
From source file:hr.fer.zemris.vhdllab.view.explorer.ProjectExplorerView.java
@Override protected JComponent createControl() { CommandManager commandManager = getActiveWindow().getCommandManager(); commandManager.createCommandGroup("hierarchyMenu", new String[] { hierarchyXUsesYCommand.getId(), hierarchyXUsedByYCommand.getId(), hierarchyFlatCommand.getId() }); CommandGroup commandGroup = commandManager.createCommandGroup("treePopupMenu", new String[] { "newMenu", "separator", openCommand.getId(), "separator", compileCommand.getId(), simulateCommand.getId(), viewVhdlCommand.getId(), "separator", deleteCommand.getId(), "separator", "hierarchyMenu" }); popupMenu = commandGroup.createPopupMenu(); root = new DefaultMutableTreeNode("vhdllab-root"); model = new DefaultTreeModel(root); model.addTreeModelListener(new TreeModelAdapter() { @Override/*from w w w . j a va 2 s .co m*/ public void treeNodesInserted(TreeModelEvent e) { Object[] children = e.getChildren(); if (children.length > 0) { Object child = children[children.length - 1]; TreePath path = e.getTreePath().pathByAddingChild(child); tree.setSelectionPath(path); } } }); tree = new JTree(model); tree.setRootVisible(false); tree.setShowsRootHandles(true); // tree is expanded manually in OpenFileOrShowPopupMenuListener tree.setToggleClickCount(0); tree.setCellRenderer(new WorkspaceCellRenderer()); tree.addMouseListener(new OpenFileOrShowPopupMenuListener()); tree.addTreeSelectionListener(new CommandGuard()); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); JPanel hierarchyPanel = new JPanel(); hierarchyPanel.add(createHierarchyButton(hierarchyXUsesYCommand)); hierarchyPanel.add(createHierarchyButton(hierarchyXUsedByYCommand)); hierarchyPanel.add(createHierarchyButton(hierarchyFlatCommand)); JPanel borderHierarchyPanel = new JPanel(new BorderLayout()); borderHierarchyPanel.add(hierarchyPanel, BorderLayout.WEST); JPanel control = new JPanel(new BorderLayout()); control.add(borderHierarchyPanel, BorderLayout.NORTH); control.add(new JScrollPane(tree), BorderLayout.CENTER); return control; }