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:Main.java

public Main() {
    setLayout(new GridLayout(1, 3));
    tree = new JTree(getTreeModel());
    tree.setDragEnabled(true);//  ww  w  .  j a v  a2  s.c o  m
    tree.setPreferredSize(new Dimension(200, 400));
    JScrollPane scroll = new JScrollPane();
    scroll.setViewportView(tree);

    treeModel = getTreeModel();
    JTree secondTree = new JTree(treeModel);
    secondTree.setPreferredSize(new Dimension(200, 400));
    secondTree.setTransferHandler(new TransferHandler() {
        @Override
        public boolean importData(TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }
            JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
            TreePath path = dl.getPath();
            int childIndex = dl.getChildIndex();

            String data;
            try {
                data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
            } catch (Exception e) {
                return false;
            }
            if (childIndex == -1) {
                childIndex = tree.getModel().getChildCount(path.getLastPathComponent());
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(data);
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            treeModel.insertNodeInto(newNode, parentNode, childIndex);

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

        public boolean canImport(TransferSupport support) {
            if (!support.isDrop()) {
                return false;
            }
            support.setShowDropLocation(true);
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                System.out.println("only string is supported");
                return false;
            }
            JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
            TreePath path = dl.getPath();
            if (path == null) {
                return false;
            }
            return true;
        }
    });
    JScrollPane secondScroll = new JScrollPane();
    secondScroll.setViewportView(secondTree);

    JPanel topPanel = new JPanel(new BorderLayout());
    topPanel.add(scroll, BorderLayout.CENTER);
    JPanel btmPanel = new JPanel(new BorderLayout());
    btmPanel.add(secondScroll, BorderLayout.CENTER);

    add(topPanel);
    add(btmPanel);
}

From source file:XMLTreeView.java

public void characters(char[] data, int start, int end) {
    String str = new String(data, start, end);
    if (!str.equals("") && Character.isLetter(str.charAt(0)))
        currentNode.add(new DefaultMutableTreeNode(str));
}

From source file:TreeEditTest.java

public TreeNode makeSampleTree() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("World");
    DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA");
    root.add(country);/*from  w w w  . ja va 2s.c o m*/
    DefaultMutableTreeNode state = new DefaultMutableTreeNode("California");
    country.add(state);
    DefaultMutableTreeNode city = new DefaultMutableTreeNode("San Jose");
    state.add(city);
    city = new DefaultMutableTreeNode("San Diego");
    state.add(city);
    state = new DefaultMutableTreeNode("Michigan");
    country.add(state);
    city = new DefaultMutableTreeNode("Ann Arbor");
    state.add(city);
    country = new DefaultMutableTreeNode("Germany");
    root.add(country);
    state = new DefaultMutableTreeNode("Schleswig-Holstein");
    country.add(state);
    city = new DefaultMutableTreeNode("Kiel");
    state.add(city);
    return root;
}

From source file:nosqltools.JSONUtilities.java

private DefaultMutableTreeNode makeJtree(String name, JsonNode node) {
    //instance of default mutable tree node with the root name of that object
    DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(name);
    //iterator that stores the fields of the JSON documents in the collection.
    Iterator<Entry<String, JsonNode>> iterator = node.fields();

    while (iterator.hasNext()) {
        //the iterator returns the next entry 
        Entry<String, JsonNode> entry = iterator.next();
        //the entry will be added to the tree node and formatted as key:value
        treeNode.add(makeJtree(entry.getKey() + " : " + entry.getValue(), entry.getValue()));
    }/*from  www .  j  a va2  s . c  o  m*/

    //if an array is found within an object
    if (node.isArray()) {
        for (int i = 0; i < node.size(); i++) {
            //create a child and get the information
            JsonNode child = node.get(i);

            //the isValueNode returns valid String representation of the container value, if the node is a value node else null
            if (child.isValueNode())
                treeNode.add(new DefaultMutableTreeNode(child.asText()));
            else
                treeNode.add(makeJtree(String.format("Node %d", i), child));
        }
    }

    return treeNode;
}

From source file:XMLTreeView.java

public void startElement(String uri, String qName, String lName, Attributes atts) {
    previousNode = currentNode;//from w  w  w .  j ava 2 s.c  o m
    currentNode = new DefaultMutableTreeNode(lName);
    // Add attributes as child nodes //
    attachAttributeList(currentNode, atts);
    previousNode.add(currentNode);
}

From source file:org.syncope.console.commons.RoleTreeBuilder.java

public TreeModel build(final List<RoleTO> roles) {
    DefaultMutableTreeNode fakeroot = new DefaultMutableTreeNode(new FakeRootRoleTO());

    populateSubtree(fakeroot, roles);//from w w  w . ja  v a2  s .co m

    return new DefaultTreeModel(fakeroot);
}

From source file:Main.java

private DefaultMutableTreeNode builtTreeNode(Node root) {
    DefaultMutableTreeNode dmtNode;

    dmtNode = new DefaultMutableTreeNode(root.getNodeName());
    NodeList nodeList = root.getChildNodes();
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);

        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            if (tempNode.hasChildNodes()) {
                dmtNode.add(builtTreeNode(tempNode));
            }/*  ww w .  j av a  2 s.c  o  m*/
        }
    }
    return dmtNode;
}

From source file:com.mindcognition.mindraider.ui.swing.explorer.LabelsTree.java

public LabelsTree(NotebooksTree notebooksTree) {
    labelsRootNode = new DefaultMutableTreeNode(
            new LabelNodeUserObject(Messages.getString("ExplorerJPanel.rootNode"), 333, null));

    labelsTreeModel = new DefaultTreeModel(labelsRootNode);
    labelsTreeModel.addTreeModelListener(new LabelsTreeModelListener());
    setModel(labelsTreeModel);/*from  w w  w.j a v a 2 s  .c om*/

    setEditable(false);
    getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    addTreeExpansionListener(new TreeExpansionListenerImplementation());
    addTreeWillExpandListener(new TreeWillExpandListenerImplementation());
    setShowsRootHandles(true);

    // tree rendered
    setCellRenderer(new LabelsTreeCellRenderer());

    // subscribe for folder custodian events e.g. folder creation
    MindRaider.labelCustodian.subscribe(this);

    // tree node selection listener
    addTreeSelectionListener(new LabelsTreeSelectionListener(this, notebooksTree));

    reloadModel();
}

From source file:components.DynamicTree.java

public DynamicTree() {
    super(new GridLayout(1, 0));

    rootNode = new DefaultMutableTreeNode("Root Node");
    treeModel = new DefaultTreeModel(rootNode);
    treeModel.addTreeModelListener(new MyTreeModelListener());
    tree = new JTree(treeModel);
    tree.setEditable(true);//  ww  w  . j ava  2s  .  co m
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setShowsRootHandles(true);

    JScrollPane scrollPane = new JScrollPane(tree);
    add(scrollPane);
}

From source file:it.unibas.spicygui.controllo.window.operator.ProjectTreeGenerator.java

public void generateTree(JTree jTree) {
    Scenarios scenarios = (Scenarios) modello.getBean(Costanti.SCENARIOS);
    DefaultMutableTreeNode nodeRoot = new DefaultMutableTreeNode(
            new TreeTopComponentAdapter(null, false, true, false));
    for (IScenario scenario : scenarios.getListaSceneri()) {
        analizzaScenario(nodeRoot, (Scenario) scenario);
    }//from  ww  w . j  av a 2s.c o m
    ((DefaultTreeModel) jTree.getModel()).setRoot(nodeRoot);
    jTree.setRootVisible(false);
    jTree.setShowsRootHandles(true);
    TreeRenderer rendererTree = new TreeRenderer();
    jTree.setCellRenderer(rendererTree);
    return;
}