List of usage examples for javax.swing.tree DefaultMutableTreeNode getUserObject
public Object getUserObject()
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(Main.class.getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTree tree = new JTree(createTreeModel(new File(System.getProperty("user.dir")), true)); tree.setCellRenderer(new DefaultTreeCellRenderer() { @Override/*from www . ja v a2s . com*/ public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus); if (value instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; if (node.getUserObject() instanceof File) { setText(((File) node.getUserObject()).getName()); } } return this; } }); tree.getSelectionModel().addTreeSelectionListener(e -> { Object object = tree.getLastSelectedPathComponent(); if (object instanceof DefaultMutableTreeNode) { Object userObject = ((DefaultMutableTreeNode) object).getUserObject(); if (userObject instanceof File) { File file = (File) userObject; System.out.println( "Selected file" + file.getAbsolutePath() + " Is directory? " + file.isDirectory()); } } }); JScrollPane pane = new JScrollPane(tree); frame.add(pane); frame.setSize(400, 600); frame.setVisible(true); }
From source file:TreeExpandSample.java
public static void main(String args[]) { String title = ("JTree Expand Sample"); JFrame frame = new JFrame(title); JTree tree = new JTree(); TreeWillExpandListener treeWillExpandListener = new TreeWillExpandListener() { public void treeWillCollapse(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException { TreePath path = treeExpansionEvent.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); if (data.equals("colors")) { throw new ExpandVetoException(treeExpansionEvent); }/*from ww w. ja v a 2 s.c o m*/ } public void treeWillExpand(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException { TreePath path = treeExpansionEvent.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); if (data.equals("sports")) { throw new ExpandVetoException(treeExpansionEvent); } } }; tree.addTreeWillExpandListener(treeWillExpandListener); JScrollPane scrollPane = new JScrollPane(tree); frame.getContentPane().add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:TreeExpansionListenerDemo.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); root.add(new DefaultMutableTreeNode("A")); root.add(new DefaultMutableTreeNode("B")); root.add(new DefaultMutableTreeNode("C")); JTree tree = new JTree(root); TreeExpansionListener treeExpandListener = new TreeExpansionListener() { public void treeExpanded(TreeExpansionEvent event) { TreePath path = event.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); System.out.println("Expanded: " + data); }//from www . j a va 2 s. c om public void treeCollapsed(TreeExpansionEvent event) { TreePath path = event.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); System.out.println("Collapsed: " + data); } }; tree.addTreeExpansionListener(treeExpandListener); JScrollPane scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); root.add(new DefaultMutableTreeNode("A")); root.add(new DefaultMutableTreeNode("B")); root.add(new DefaultMutableTreeNode("C")); JTree tree = new JTree(root); TreeWillExpandListener treeWillExpandListener = new TreeWillExpandListener() { public void treeWillCollapse(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException { TreePath path = treeExpansionEvent.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); System.out.println("WillCollapse: " + data); }/*from w w w . ja va 2 s.c o m*/ public void treeWillExpand(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException { TreePath path = treeExpansionEvent.getPath(); DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); String data = node.getUserObject().toString(); System.out.println("WillExpand: " + data); } }; tree.addTreeWillExpandListener(treeWillExpandListener); JScrollPane scrollPane = new JScrollPane(tree); frame.add(scrollPane, BorderLayout.CENTER); frame.setSize(300, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame f = new JFrame("JTree Demo"); f.setSize(260, 240);/*from w ww .j av a 2s . c o 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:Main.java
public static boolean isChildNode(DefaultMutableTreeNode parent, DefaultMutableTreeNode child) { if (parent == child) return false; DefaultMutableTreeNode node = child; while ((node = (DefaultMutableTreeNode) node.getParent()) != null) { if (node.getUserObject() == parent.getUserObject()) return true; }/*from w w w .ja v a 2s . c o m*/ return false; }
From source file:Main.java
public static DefaultMutableTreeNode sortTreeNode(DefaultMutableTreeNode root) { if (root == null) return root; int count = root.getChildCount(); if (count <= 0) return root; for (int i = 0; i < count; i++) { for (int j = count - 1; j > i; j--) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(j); String nt = node.getUserObject().toString(); DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j - 1); String np = prevNode.getUserObject().toString(); if (nt.compareToIgnoreCase(np) < 0) { root.insert(node, j - 1); root.insert(prevNode, j); }//from www . j a v a2s. c om } sortTreeNode((DefaultMutableTreeNode) root.getChildAt(i)); } for (int i = 0; i < count; i++) { for (int j = count - 1; j > i; j--) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(j); DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j - 1); if (prevNode.isLeaf() && !node.isLeaf()) { root.insert(node, j - 1); root.insert(prevNode, j); } } } return root; }
From source file:Main.java
private static DefaultMutableTreeNode addCloneNode(DefaultMutableTreeNode srcNode, DefaultMutableTreeNode root) { DefaultMutableTreeNode clone = new DefaultMutableTreeNode(srcNode.getUserObject()); root.add(clone);//from w w w .j av a 2 s . c o m for (int i = 0; i < srcNode.getChildCount(); i++) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) srcNode.getChildAt(i); addCloneNode(child, clone); } return clone; }
From source file:Main.java
public static DefaultMutableTreeNode cloneTreeNode(DefaultMutableTreeNode srcNode) { if (srcNode == null) return null; DefaultMutableTreeNode node = new DefaultMutableTreeNode(srcNode.getUserObject()); for (int i = 0; i < srcNode.getChildCount(); i++) { DefaultMutableTreeNode child = (DefaultMutableTreeNode) srcNode.getChildAt(i); addCloneNode(child, node);/*ww w . ja va 2 s . c o m*/ } return node; }
From source file:MyData.java
public static MyData getQuestionFromValue(Object value) { if (value instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; Object userObject = node.getUserObject(); if (userObject instanceof Main) { return (MyData) userObject; }/* w w w . j a v a 2 s. c om*/ } return null; }