Example usage for javax.swing JTree getModel

List of usage examples for javax.swing JTree getModel

Introduction

In this page you can find the example usage for javax.swing JTree getModel.

Prototype

public TreeModel getModel() 

Source Link

Document

Returns the TreeModel that is providing the data.

Usage

From source file:Main.java

public static void main(String[] args) {
    JTree tree = new JTree();
    Enumeration en = ((DefaultMutableTreeNode) tree.getModel().getRoot()).preorderEnumeration();
    while (en.hasMoreElements()) {
        TreePath path = new TreePath(((DefaultMutableTreeNode) en.nextElement()).getPath());
        String text = path.toString();
        System.out.println(text);
    }/*from  w w  w .ja va2 s .c  o m*/
}

From source file:TreeNodeRemove.java

public static void main(String[] argv) {
    Vector<String> v = new Vector<String>();
    v.add("a");/*from   w  ww  . j a v a  2 s .c om*/
    v.add("b");
    v.add("c");
    JTree tree = new JTree(v);
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();

    int startRow = 0;
    String prefix = "b";
    TreePath path = tree.getNextMatch(prefix, startRow, Position.Bias.Forward);
    MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();

    model.removeNodeFromParent(node);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setSize(380, 320);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Swing Package Hierarchy");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Vector rootVector = new TreeVector("A", new String[] { "a" });
    JTree tree = new JTree(rootVector);
    tree.setRootVisible(true);//from  ww w.  j  av  a 2  s.co m
    TreeModel model = tree.getModel();
    model.valueForPathChanged(new TreePath(model.getRoot()), "javax.swing");
    ((DefaultTreeModel) model).reload();

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

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);//from  ww w.  j a v a2  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:DndTree.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel top = new JPanel(new BorderLayout());
    JLabel dragLabel = new JLabel("Drag me:");
    JTextField text = new JTextField();
    text.setDragEnabled(true);/*from   ww  w . j av a 2s .co m*/
    top.add(dragLabel, BorderLayout.WEST);
    top.add(text, BorderLayout.CENTER);
    f.add(top, BorderLayout.NORTH);

    final JTree tree = new JTree();
    final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    tree.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            return dropLocation.getPath() != null;
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            TreePath path = dropLocation.getPath();
            Transferable transferable = support.getTransferable();

            String transferData;
            try {
                transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (IOException e) {
                return false;
            } catch (UnsupportedFlavorException e) {
                return false;
            }

            int childIndex = dropLocation.getChildIndex();
            if (childIndex == -1) {
                childIndex = model.getChildCount(path.getLastPathComponent());
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData);
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            model.insertNodeInto(newNode, parentNode, childIndex);

            TreePath newPath = path.pathByAddingChild(newNode);
            tree.makeVisible(newPath);
            tree.scrollRectToVisible(tree.getPathBounds(newPath));
            return true;
        }
    });

    JScrollPane pane = new JScrollPane(tree);
    f.add(pane, BorderLayout.CENTER);

    tree.setDropMode(DropMode.USE_SELECTION);
    f.setSize(300, 400);
    f.setVisible(true);
}

From source file:DndTree.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel top = new JPanel(new BorderLayout());
    JLabel dragLabel = new JLabel("Drag me:");
    JTextField text = new JTextField();
    text.setDragEnabled(true);//from   ww w .ja  v  a  2 s .c o  m
    top.add(dragLabel, BorderLayout.WEST);
    top.add(text, BorderLayout.CENTER);
    f.add(top, BorderLayout.NORTH);

    final JTree tree = new JTree();
    final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    tree.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            return dropLocation.getPath() != null;
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            TreePath path = dropLocation.getPath();
            Transferable transferable = support.getTransferable();

            String transferData;
            try {
                transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (IOException e) {
                return false;
            } catch (UnsupportedFlavorException e) {
                return false;
            }

            int childIndex = dropLocation.getChildIndex();
            if (childIndex == -1) {
                childIndex = model.getChildCount(path.getLastPathComponent());
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData);
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            model.insertNodeInto(newNode, parentNode, childIndex);

            TreePath newPath = path.pathByAddingChild(newNode);
            tree.makeVisible(newPath);
            tree.scrollRectToVisible(tree.getPathBounds(newPath));
            return true;
        }
    });

    JScrollPane pane = new JScrollPane(tree);
    f.add(pane, BorderLayout.CENTER);

    tree.setDropMode(DropMode.INSERT);
    f.setSize(300, 400);
    f.setVisible(true);
}

From source file:DndTree.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel top = new JPanel(new BorderLayout());
    JLabel dragLabel = new JLabel("Drag me:");
    JTextField text = new JTextField();
    text.setDragEnabled(true);//from   ww  w . ja  va 2s. c  om
    top.add(dragLabel, BorderLayout.WEST);
    top.add(text, BorderLayout.CENTER);
    f.add(top, BorderLayout.NORTH);

    final JTree tree = new JTree();
    final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    tree.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            return dropLocation.getPath() != null;
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            TreePath path = dropLocation.getPath();
            Transferable transferable = support.getTransferable();

            String transferData;
            try {
                transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (IOException e) {
                return false;
            } catch (UnsupportedFlavorException e) {
                return false;
            }

            int childIndex = dropLocation.getChildIndex();
            if (childIndex == -1) {
                childIndex = model.getChildCount(path.getLastPathComponent());
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData);
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            model.insertNodeInto(newNode, parentNode, childIndex);

            TreePath newPath = path.pathByAddingChild(newNode);
            tree.makeVisible(newPath);
            tree.scrollRectToVisible(tree.getPathBounds(newPath));
            return true;
        }
    });

    JScrollPane pane = new JScrollPane(tree);
    f.add(pane, BorderLayout.CENTER);

    tree.setDropMode(DropMode.ON_OR_INSERT);
    f.setSize(300, 400);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new BorderLayout());
    final JTree tree = new JTree();
    panel.add(new JScrollPane(tree));
    JButton btn = new JButton("Press Me");
    btn.addActionListener(et -> {//  ww  w .j a  v  a  2s . c o  m
        for (Enumeration e = ((TreeNode) tree.getModel().getRoot()).children(); e.hasMoreElements();) {
            TreeNode tn = (TreeNode) e.nextElement();
            tree.expandPath(new TreePath(((DefaultTreeModel) tree.getModel()).getPathToRoot(tn)));
        }
    });
    panel.add(btn, BorderLayout.SOUTH);
    JFrame frame = new JFrame("");
    frame.getContentPane().add(panel);
    frame.setSize(300, 300);
    frame.setLocation(100, 100);
    frame.pack();
    frame.show();
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame("JTree Demo");
    f.setSize(260, 240);//from ww w .  jav a 2s. 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:Main.java

public static void visitAllNodes(JTree tree) {
    TreeNode root = (TreeNode) tree.getModel().getRoot();
    visitAllNodes(root);
}