List of usage examples for javax.swing.tree DefaultMutableTreeNode DefaultMutableTreeNode
public DefaultMutableTreeNode(Object userObject)
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 ww w . j a v a2 s. 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:Main.java
public static DefaultTreeModel buildDemoModel() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); DefaultMutableTreeNode childNode = new DefaultMutableTreeNode("A"); childNode.add(new DefaultMutableTreeNode("A1")); childNode.add(new DefaultMutableTreeNode("A2")); root.add(childNode);/*from w w w . jav a2 s. c o m*/ childNode = new DefaultMutableTreeNode("B"); childNode.add(new DefaultMutableTreeNode("B1")); childNode.add(new DefaultMutableTreeNode("B2")); root.add(childNode); return new DefaultTreeModel(root); }
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 ww . j av a2s. c o 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:TestTree.java
public void init() { // Build up a bunch of TreeNodes. We use DefaultMutableTreeNode because // the//from w w w.j a v a 2 s. c om // DefaultTreeModel can use it to build a complete tree. DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot"); DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1"); DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2"); // Build our tree model starting at the root node, and then make a JTree // out // of that. treeModel = new DefaultTreeModel(root); tree = new JTree(treeModel); // Build the tree up from the nodes we created. treeModel.insertNodeInto(subroot, root, 0); // Or, more succinctly: subroot.add(leaf1); root.add(leaf2); // Display it. getContentPane().add(tree, BorderLayout.CENTER); }
From source file:SortTreeDemo.java
public SortTreeDemo(String startDir) { super("SortTreeModel Demonstration"); setSize(300, 400);// w w w . j av a 2 s.c o m setDefaultCloseOperation(EXIT_ON_CLOSE); PrettyFile f = new PrettyFile(startDir); DefaultMutableTreeNode root = new DefaultMutableTreeNode(f); SortTreeModel model = new SortTreeModel(root, new TreeStringComparator()); fillModel(model, root); JTree tree = new JTree(model); getContentPane().add(new JScrollPane(tree)); }
From source file:AncestorTree.java
public AncestorTree() { super("Ancestor Tree"); setSize(400, 300);/*from w ww . j a v a2s . com*/ DefaultMutableTreeNode top = new DefaultMutableTreeNode(new IconData(ICON_SELF, "Myself")); addAncestors(top); m_model = new DefaultTreeModel(top); m_tree = new JTree(m_model); m_tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); m_tree.setShowsRootHandles(true); m_tree.setEditable(true); m_renderer = new IconCellRenderer(); m_tree.setCellRenderer(m_renderer); m_editor = new IconCellEditor(m_tree); m_tree.setCellEditor(m_editor); m_tree.setInvokesStopCellEditing(true); m_tree.addMouseListener(new TreeExpander()); JScrollPane s = new JScrollPane(); s.getViewport().add(m_tree); getContentPane().add(s, BorderLayout.CENTER); WindowListener wndCloser = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; addWindowListener(wndCloser); setVisible(true); }
From source file:XMLTreeView.java
public XMLTreeView(JFrame frame) { frame.getContentPane().setLayout(new BorderLayout()); DefaultMutableTreeNode top = new DefaultMutableTreeNode(file); // DefaultMutableTreeNode top = new DefaultMutableTreeNode("XML Document"); saxTree = new SAXTreeBuilder(top); try {//from w w w .j av a2s .c om SAXParser saxParser = new SAXParser(); saxParser.setContentHandler(saxTree); saxParser.parse(new InputSource(new FileInputStream(file))); } catch (Exception ex) { top.add(new DefaultMutableTreeNode(ex.getMessage())); } JTree tree = new JTree(saxTree.getTree()); JScrollPane scrollPane = new JScrollPane(tree); frame.getContentPane().add("Center", scrollPane); frame.setVisible(true); }
From source file:Main.java
public Main() { super(BoxLayout.Y_AXIS); glassPane.setOpaque(false);/*from www .j a va2 s . co m*/ DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root"); for (int i = 0; i < 14000; i++) { DefaultMutableTreeNode node = new DefaultMutableTreeNode("Root" + i); node.add(new DefaultMutableTreeNode("Child" + i)); root.add(node); } final JTree tree = new JTree(root); tree.setRootVisible(false); final JScrollPane pane = new JScrollPane(tree); add(pane); JButton button = new JButton("Expand"); button.addActionListener(e -> { Thread t = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } TreePath[] paths = tree.getSelectionPaths(); if (paths == null || paths.length == 0) { glassPane.setVisible(false); return; } tree.setSelectionPath(paths[0]); for (int i = 0; i < paths.length; i++) { tree.expandPath(paths[i]); } glassPane.setVisible(false); } }); getRootPane().setGlassPane(glassPane); glassPane.setVisible(true); t.start(); }); add(button); glassPane.addMouseWheelListener(e -> { for (MouseWheelListener mwl : pane.getMouseWheelListeners()) { mwl.mouseWheelMoved(e); } }); }
From source file:FSTest.java
public FSTest() { super("FSTree Component Test"); setSize(300, 300);/*from w w w . ja v a 2s .com*/ setDefaultCloseOperation(EXIT_ON_CLOSE); FSTree fst = new FSTree(new DefaultMutableTreeNode("Starter")); getContentPane().add(new JScrollPane(fst)); setVisible(true); }
From source file:Main.java
private DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) { String curPath = dir.getPath(); DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath); if (curTop != null) { curTop.add(curDir);// w w w . jav a 2 s . c om } Vector<String> ol = new Vector<String>(); String[] tmp = dir.list(); for (int i = 0; i < tmp.length; i++) { ol.addElement(tmp[i]); } Collections.sort(ol, String.CASE_INSENSITIVE_ORDER); File f; Vector<Object> files = new Vector<Object>(); for (int i = 0; i < ol.size(); i++) { String thisObject = ol.elementAt(i); String newPath; if (curPath.equals(".")) { newPath = thisObject; } else { newPath = curPath + File.separator + thisObject; } if ((f = new File(newPath)).isDirectory()) { addNodes(curDir, f); } else { files.addElement(thisObject); } } for (int fnum = 0; fnum < files.size(); fnum++) { curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum))); } return curDir; }