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

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

    //Create the nodes.
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);//from  w  w w .ja  va 2  s .  com

    //Create a tree that allows one selection at a time.
    tree = new JTree(top);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    //Listen for when the selection changes.
    tree.addTreeSelectionListener(this);

    if (playWithLineStyle) {
        System.out.println("line style = " + lineStyle);
        tree.putClientProperty("JTree.lineStyle", lineStyle);
    }

    //Create the scroll pane and add the tree to it. 
    JScrollPane treeView = new JScrollPane(tree);

    //Create the HTML viewing pane.
    htmlPane = new JEditorPane();
    htmlPane.setEditable(false);
    initHelp();
    JScrollPane htmlView = new JScrollPane(htmlPane);

    //Add the scroll panes to a split pane.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setTopComponent(treeView);
    splitPane.setBottomComponent(htmlView);

    Dimension minimumSize = new Dimension(100, 50);
    htmlView.setMinimumSize(minimumSize);
    treeView.setMinimumSize(minimumSize);
    splitPane.setDividerLocation(100); //XXX: ignored in some releases
                                       //of Swing. bug 4101306
                                       //workaround for bug 4101306:
                                       //treeView.setPreferredSize(new Dimension(100, 100)); 

    splitPane.setPreferredSize(new Dimension(500, 300));

    //Add the split pane to this panel.
    add(splitPane);
}

From source file:ws.moor.bt.grapher.Grapher.java

private static TreeNode convertToTree(List<String> names) {
    Map<String, DefaultMutableTreeNode> existingNodes = new HashMap<String, DefaultMutableTreeNode>();

    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Counters");

    for (String name : names) {
        DefaultMutableTreeNode bestFittingParent = rootNode;
        String[] parts = name.split("\\.|@");
        StringBuilder prefix = new StringBuilder();
        for (int i = 0; i < parts.length - 1; i++) {
            prefix.append(parts[i]);/*from   w  ww.j a  v a 2 s .c  o  m*/
            DefaultMutableTreeNode parent = existingNodes.get(prefix.toString());
            if (parent != null) {
                bestFittingParent = parent;
            } else {
                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(parts[i]);
                bestFittingParent.add(newNode);
                existingNodes.put(prefix.toString(), newNode);
                bestFittingParent = newNode;
            }
        }

        DefaultMutableTreeNode currentNode = new DefaultMutableTreeNode(new NodeValue(name));
        bestFittingParent.add(currentNode);
        currentNode.setAllowsChildren(false);
    }
    return rootNode;
}

From source file:edu.ucla.stat.SOCR.chart.ChartTree.java

/**
 * Creates the tree node for the pie chart demos.
 * //from  w  w w.j  a v a  2s.  c  om
 * @return A populated tree node.
 */
private MutableTreeNode createPieChartsNode() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Pie Charts");

    DefaultMutableTreeNode n1 = new DefaultMutableTreeNode(
            new DemoDescription("edu.ucla.stat.SOCR.chart.demo.PieChartDemo1", "PieChartDemo1"));
    DefaultMutableTreeNode n2 = new DefaultMutableTreeNode(
            new DemoDescription("edu.ucla.stat.SOCR.chart.demo.PieChartDemo2", "PieChartDemo2"));
    DefaultMutableTreeNode n3 = new DefaultMutableTreeNode(
            new DemoDescription("edu.ucla.stat.SOCR.chart.demo.PieChartDemo4", "PieChartDemo4"));
    DefaultMutableTreeNode n4 = new DefaultMutableTreeNode(
            new DemoDescription("edu.ucla.stat.SOCR.chart.demo.PieChart3DDemo1", "PieChart3DDemo1"));
    DefaultMutableTreeNode n5 = new DefaultMutableTreeNode(
            new DemoDescription("edu.ucla.stat.SOCR.chart.demo.PieChart3DDemo2", "PieChart3DDemo2"));
    DefaultMutableTreeNode n6 = new DefaultMutableTreeNode(
            new DemoDescription("edu.ucla.stat.SOCR.chart.demo.PieChart3DDemo3", "PieChart3DDemo3"));
    DefaultMutableTreeNode n7 = new DefaultMutableTreeNode(new DemoDescription(
            "edu.ucla.stat.SOCR.chart.demo.MultiplePieChartDemo1", "MultiplePieChartDemo1"));
    DefaultMutableTreeNode n8 = new DefaultMutableTreeNode(
            new DemoDescription("edu.ucla.stat.SOCR.chart.demo.RingChartDemo1", "RingChartDemo1"));

    root.add(n1);
    root.add(n2);
    root.add(n3);
    root.add(n4);
    root.add(n5);
    root.add(n6);
    // root.add(n7);
    root.add(n8);
    return root;
}

From source file:it.unibas.spicygui.controllo.datasource.operators.GenerateSchemaTree.java

private void visitNode(INode node) {
    DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(
            new TreeNodeAdapter(node, false, false, dataSource.getType()));
    if (logger.isDebugEnabled()) {
        logger.debug("Creato nuovo nodo: " + treeNode);
    }//  www. j a  va 2  s  .  c  om
    if (treeRoot == null) {
        treeRoot = treeNode;
    } else {
        currentTreeNode.add(treeNode);
    }
    currentTreeNode = treeNode;
    for (INode child : node.getChildren()) {
        child.accept(this);
        currentTreeNode = treeNode;
    }
}

From source file:uk.co.markfrimston.tasktree.TaskTree.java

public TaskTree(String filePath) {
    this.filePath = filePath;

    this.root = new DefaultMutableTreeNode("root");
    this.treeModel = new DefaultTreeModel(root);
}

From source file:FileTree.java

/** Add nodes from under "dir" into curTop. Highly recursive. */
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
    String curPath = dir.getPath();
    DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
    if (curTop != null) { // should only be null at root
        curTop.add(curDir);/*from   w  w w  . ja va  2  s  . com*/
    }
    Vector ol = new Vector();
    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 files = new Vector();
    // Make two passes, one for Dirs and one for Files. This is #1.
    for (int i = 0; i < ol.size(); i++) {
        String thisObject = (String) 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);
    }
    // Pass two: for files.
    for (int fnum = 0; fnum < files.size(); fnum++)
        curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
    return curDir;
}

From source file:it.unibas.spicygui.controllo.datasource.operators.GenerateInstanceTree.java

public void visitAttributeNode(AttributeNode node) {
    DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(
            new TreeNodeAdapter(node, dataSource.getType()));
    if (logger.isDebugEnabled())
        logger.debug("Creato nuovo nodo: " + treeNode);
    currentTreeNode.add(treeNode);//w w w. j av a 2 s .com
}

From source file:gr.aueb.mipmapgui.controller.datasource.operators.GenerateInstanceTree.java

private void visitNode(INode node) {
    //web - check before change
    //DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(new TreeNodeAdapter(node, dataSource.getType()));

    DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(node);
    ////if (logger.isDebugEnabled()) logger.debug("Creato nuovo nodo: " + treeNode);
    if (treeRoot == null) {
        treeRoot = treeNode;/*w ww.  j  a  va 2  s . com*/
    } else {
        currentTreeNode.add(treeNode);
    }
    currentTreeNode = treeNode;
    for (INode child : node.getChildren()) {
        child.accept(this);
        currentTreeNode = treeNode;
    }
}

From source file:SAXTreeValidator.java

/**
 * <p> This will construct the tree using Swing. </p>
 *
 * @param filename <code>String</code> path to XML document.
 *///from   w  w w.  ja v  a 2 s . c  o m
public void init(String xmlURI) throws IOException, SAXException {
    DefaultMutableTreeNode base = new DefaultMutableTreeNode("XML Document: " + xmlURI);

    // Build the tree model
    defaultTreeModel = new DefaultTreeModel(base);
    jTree = new JTree(defaultTreeModel);

    // Construct the tree hierarchy
    buildTree(defaultTreeModel, base, xmlURI);

    // Display the results
    getContentPane().add(new JScrollPane(jTree), BorderLayout.CENTER);
}

From source file:gov.nij.er.ui.RecordTreeModel.java

/**
 * Update the model with data and notify listeners
 *//*from   w ww. j  a va 2 s . c om*/
public void updateModel() {
    root = new DefaultMutableTreeNode(ROOT_NODE_LABEL);
    if (records != null) {
        Collections.sort(records, recordSorter);
        for (RecordWrapper record : records) {

            DefaultMutableTreeNode node = new RecordTreeNode(record);
            root.add(node);
        }
        LOG.debug("Initializing tree model with new records");
        for (TreeModelListener l : listeners) {
            l.treeStructureChanged(new TreeModelEvent(this, new Object[] { root, }));
        }
    }
}