List of usage examples for javax.swing.tree TreePath getPath
public Object[] getPath()
From source file:MainClass.java
public static void main(String args[]) { JFrame frame = new JFrame("Traverse Tree"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTree tree = new JTree(); tree.setRootVisible(true);// ww w . j a v a 2 s .c om TreeModel model = tree.getModel(); Object rootObject = model.getRoot(); if ((rootObject != null) && (rootObject instanceof DefaultMutableTreeNode)) { DefaultMutableTreeNode r = (DefaultMutableTreeNode) rootObject; printDescendents(r); Enumeration breadth = r.breadthFirstEnumeration(); Enumeration depth = r.depthFirstEnumeration(); Enumeration preOrder = r.preorderEnumeration(); printEnumeration(breadth, "Breadth"); printEnumeration(depth, "Depth"); printEnumeration(preOrder, "Pre"); } TreeSelectionListener treeSelectionListener = new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent treeSelectionEvent) { JTree treeSource = (JTree) treeSelectionEvent.getSource(); TreePath path = treeSource.getSelectionPath(); System.out.println(path); System.out.println(path.getPath()); System.out.println(path.getParentPath()); System.out.println(((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject()); System.out.println(path.getPathCount()); } }; tree.addTreeSelectionListener(treeSelectionListener); JScrollPane scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 400); frame.setVisible(true); }
From source file:Main.java
/** * Determines whether a path exists in a given tree model. For this to work, * the TreeModel.getIndexOfChild() method has to be correctly implemented *///from w ww . ja v a 2 s . co m public static boolean existsInModel(TreeModel model, TreePath path) { Object[] objects = path.getPath(); if (!objects[0].equals(model.getRoot())) return false; Object prev = objects[0]; for (int i = 1; i < objects.length; i++) { if (model.getIndexOfChild(prev, objects[i]) < 0) return false; prev = objects[i]; } return true; }
From source file:TreeUtil.java
public static String pathToString(TreePath path) { if (path == null) { return null; }//from ww w. j av a 2s .c o m Object[] obj = path.getPath(); String res = ""; for (int i = 0; i < obj.length; i++) { res = res + "." + obj[i].toString(); } if (res.length() > 0) { res = res.substring(1); } return res; }
From source file:org.spf4j.ui.TSDBViewJInternalFrame.java
@edu.umd.cs.findbugs.annotations.SuppressWarnings("CLI_CONSTANT_LIST_INDEX") private static List<String> getSelectedTables(@Nullable final TreePath[] selectionPaths) { if (selectionPaths == null) { return Collections.EMPTY_LIST; }// www . ja v a 2s . c om List<String> result = new ArrayList<String>(); for (TreePath path : selectionPaths) { Object[] pathArr = path.getPath(); if (pathArr.length < 2) { continue; } DefaultMutableTreeNode colNode = (DefaultMutableTreeNode) pathArr[1]; int depth = colNode.getDepth(); String tableName; if (depth == 1) { result.add((String) colNode.getUserObject()); } else { Enumeration childEnum = colNode.children(); while (childEnum.hasMoreElements()) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) childEnum.nextElement(); tableName = Pair.of((String) colNode.getUserObject(), (String) child.getUserObject()) .toString(); result.add(tableName); } } } return result; }
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 www. jav a 2 s . com*/ 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:TreeUtil.java
public void valueChanged(TreeSelectionEvent e) { TreePath path = e.getPath(); Object[] items = path.getPath(); if (items.length < 1) { return;//ww w. j a va 2s . c om } String cmd = ""; for (int i = 0; i < items.length; i++) { cmd = cmd + "." + items[i].toString(); } if (cmd.length() > 0) { cmd = cmd.substring(1); } fireActionEvent(new ActionEvent(this, TREE, cmd)); }
From source file:gov.nij.er.ui.RecordTreeModel.java
/** * Get the IDs of the records represented by the specified path * @param selectedPath the path to search * @return the ids of the records at that path *///from www .ja v a2s . c o m public Set<String> getRecordIdsForPath(TreePath selectedPath) { Set<String> ret = new HashSet<String>(); Object[] pathArray = selectedPath.getPath(); if (pathArray.length > 1) { DefaultMutableTreeNode recordNode = (DefaultMutableTreeNode) pathArray[1]; @SuppressWarnings("unchecked") Enumeration<TreeNode> descendants = recordNode.breadthFirstEnumeration(); while (descendants.hasMoreElements()) { TreeNode node = descendants.nextElement(); if (node instanceof IdNode) { ret.add(((IdNode) node).getId()); } } } LOG.debug("Record ids for path: " + ret); return ret; }
From source file:au.org.ala.delta.editor.ui.CharacterTree.java
/** * Returns the character number of the Character at the supplied TreePath. * //from w w w. ja v a 2s. c o m * @param path the TreePath of interest. * @return the character number representing the drop position. */ private int pathToCharacterNumber(TreePath path) { if (path != null) { Object[] nodes = path.getPath(); if (nodes[1] instanceof CharacterTreeNode) { return ((CharacterTreeNode) nodes[1]).getCharacter().getCharacterId(); } } return -1; }
From source file:com.haulmont.cuba.desktop.gui.data.TreeModelAdapter.java
public TreeModelAdapter(HierarchicalDatasource datasource, CaptionMode captionMode, String captionProperty, boolean autoRefresh) { this.datasource = datasource; this.captionMode = captionMode; setCaptionProperty(captionProperty); this.autoRefresh = autoRefresh; this.metadataTools = AppBeans.get(MetadataTools.NAME); collectionChangeListener = e -> { switch (e.getOperation()) { case CLEAR: case REFRESH: case ADD: case REMOVE: Object[] path = { getRoot() }; for (TreeModelListener listener : treeModelListeners) { TreeModelEvent ev = new TreeModelEvent(this, path); listener.treeStructureChanged(ev); }/*from w w w .j a v a 2 s .c o m*/ break; case UPDATE: for (Object item : e.getItems()) { TreePath treePath = getTreePath(item); for (TreeModelListener listener : treeModelListeners) { TreeModelEvent ev = new TreeModelEvent(this, treePath.getPath()); listener.treeNodesChanged(ev); } } break; } }; //noinspection unchecked datasource.addCollectionChangeListener( new WeakCollectionChangeListener(datasource, collectionChangeListener)); }
From source file:brainflow.app.presentation.controls.FileObjectGroupSelector.java
public FileObjectGroupSelector(FileObject root) throws FileSystemException { if (root.getType() != FileType.FOLDER) { throw new IllegalArgumentException("root file must be a directory"); }/* www . ja v a 2s .c o m*/ rootFolder = root; explorer = new FileExplorer(rootFolder); explorer.getJTree().setRootVisible(false); explorer.getJTree().getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); setLayout(new BorderLayout()); Box topPanel = new Box(BoxLayout.X_AXIS); topPanel.setBorder(BorderFactory.createEmptyBorder(8, 12, 8, 8)); topPanel.add(regexLabel); topPanel.add(regexField); topPanel.add(Box.createHorizontalStrut(4)); topPanel.add(searchType); add(topPanel, BorderLayout.NORTH); //JPanel mainPanel = new JPanel(); //MigLayout layout = new MigLayout(""); // mainPanel.setLayout(layout); //add(regexLabel); //add(regexField, "gap left 0, growx"); //add(findButton, "align right, wrap"); //add(searchType, "wrap"); //add(createSplitPane(), "span 3, grow, wrap"); add(createSplitPane(), BorderLayout.CENTER); depthSpinner.setMaximumSize(new Dimension(50, 200)); depthSpinner.setModel(new SpinnerNumberModel(recursiveDepth, 0, 5, 1)); JPanel bottomPanel = new JPanel(); MigLayout layout = new MigLayout("", "[][grow]", "[][]"); bottomPanel.setLayout(layout); //bottomPanel.setBorder(BorderFactory.createEmptyBorder(4,12,8,8)); bottomPanel.add(new JLabel("Root Folder: ")); rootField.setEditable(false); rootField.setText(root.getName().getBaseName()); bottomPanel.add(rootField, "wrap, width 150:150:150"); bottomPanel.add(new JLabel("Search Depth: "), "gap top 8"); bottomPanel.add(depthSpinner, "width 35:45:55, wrap"); add(bottomPanel, BorderLayout.SOUTH); explorer.addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { TreePath path = e.getPath(); Object[] obj = path.getPath(); Object lastNode = obj[obj.length - 1]; if (lastNode instanceof FileExplorer.FileObjectNode) { FileExplorer.FileObjectNode fnode = (FileExplorer.FileObjectNode) lastNode; try { if (fnode.getFileObject().getType() == FileType.FOLDER) { rootField.setText(fnode.getFileObject().getName().getBaseName()); rootFolder = fnode.getFileObject(); updateFileList(); } } catch (FileSystemException ex) { throw new RuntimeException(ex); } } } }); depthSpinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { recursiveDepth = ((Number) depthSpinner.getValue()).intValue(); updateFileList(); } }); regexField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { updateFileList(); System.out.println(Arrays.toString(explorer.getSelectedNodes().toArray())); } }); }