Example usage for javax.swing.tree DefaultMutableTreeNode DefaultMutableTreeNode

List of usage examples for javax.swing.tree DefaultMutableTreeNode DefaultMutableTreeNode

Introduction

In this page you can find the example usage for javax.swing.tree DefaultMutableTreeNode DefaultMutableTreeNode.

Prototype

public DefaultMutableTreeNode(Object userObject) 

Source Link

Document

Creates a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.

Usage

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);// w ww . j av  a 2 s  . 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.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);/*  w  w  w.j av  a 2  s  .  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:SimpleClient.java

public static void main(String argv[]) {
    boolean usage = false;

    for (int optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-L")) {
            url.addElement(argv[++optind]);
        } else if (argv[optind].startsWith("-")) {
            usage = true;//from w  w  w  .  j  ava2s  . c  om
            break;
        } else {
            usage = true;
            break;
        }
    }

    if (usage || url.size() == 0) {
        System.out.println("Usage: SimpleClient -L url");
        System.out.println("   where url is protocol://username:password@hostname/");
        System.exit(1);
    }

    try {
        // Set up our Mailcap entries.  This will allow the JAF
        // to locate our viewers.
        File capfile = new File("simple.mailcap");
        if (!capfile.isFile()) {
            System.out.println("Cannot locate the \"simple.mailcap\" file.");
            System.exit(1);
        }

        CommandMap.setDefaultCommandMap(new MailcapCommandMap(new FileInputStream(capfile)));

        JFrame frame = new JFrame("Simple JavaMail Client");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // Get a Store object
        SimpleAuthenticator auth = new SimpleAuthenticator(frame);
        Session session = Session.getDefaultInstance(System.getProperties(), auth);
        //session.setDebug(true);

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

        // create a node for each store we have
        for (Enumeration e = url.elements(); e.hasMoreElements();) {
            String urlstring = (String) e.nextElement();
            URLName urln = new URLName(urlstring);
            Store store = session.getStore(urln);

            StoreTreeNode storenode = new StoreTreeNode(store);
            root.add(storenode);
        }

        DefaultTreeModel treeModel = new DefaultTreeModel(root);
        JTree tree = new JTree(treeModel);
        tree.addTreeSelectionListener(new TreePress());

        /* Put the Tree in a scroller. */
        JScrollPane sp = new JScrollPane();
        sp.setPreferredSize(new Dimension(250, 300));
        sp.getViewport().add(tree);

        /* Create a double buffered JPanel */
        JPanel sv = new JPanel(new BorderLayout());
        sv.add("Center", sp);

        fv = new FolderViewer(null);

        JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sv, fv);
        jsp.setOneTouchExpandable(true);
        mv = new MessageViewer();
        JSplitPane jsp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jsp, mv);
        jsp2.setOneTouchExpandable(true);

        frame.getContentPane().add(jsp2);
        frame.pack();
        frame.show();

    } catch (Exception ex) {
        System.out.println("SimpletClient caught exception");
        ex.printStackTrace();
        System.exit(1);
    }
}

From source file:Main.java

static DefaultMutableTreeNode createTreeModel(File file, boolean recursive) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(file);
    if (file.isDirectory() && recursive) {
        File[] children = file.listFiles();
        if (children != null) {
            for (File f : children) {
                node.add(createTreeModel(f, recursive));
            }/*from  ww  w  .  j av a 2 s. com*/
        }
    }
    return node;
}

From source file:Main.java

private static DefaultTreeModel getTreeModel() {
    return new DefaultTreeModel(new DefaultMutableTreeNode("Root object"));
}

From source file:Main.java

private static DefaultTreeModel buildDemoModel() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

    root.add(new DefaultMutableTreeNode("A"));
    root.add(new DefaultMutableTreeNode("B"));
    root.add(new DefaultMutableTreeNode("C"));

    return new DefaultTreeModel(root);
}

From source file:Main.java

static DefaultMutableTreeNode pathToTreeNode(Object[] objects, int offset) {
    if (objects == null || objects.length == 0 || offset >= objects.length || offset < 0) {
        return null;
    }//from  ww  w  .j a  va2s  . co m
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(objects[offset]);
    DefaultMutableTreeNode child = pathToTreeNode(objects, offset + 1);
    if (child != null) {
        node.add(child);
    }
    return node;
}

From source file:Main.java

private static void fillTree(DefaultMutableTreeNode parent, int level, String label) {
    for (int i = 0; i < 5; i++) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(label + " " + i);
        parent.add(node);/*from ww w  .j  av a  2  s  .  c  o m*/
        if (level > 0) {
            fillTree(node, level - 1, label);
        }
    }
}

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);//from  ww w.ja  v  a 2 s. com
    }
    return node;
}

From source file:Main.java

private static TreeModel getTreeModel() {
    return new DefaultTreeModel(getNodes(
            new DefaultMutableTreeNode("<html>Root <a href=\"http://www.google.be\">Google</a></html>"), 5));
}