Example usage for javax.swing.tree DefaultTreeModel DefaultTreeModel

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

Introduction

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

Prototype

@ConstructorProperties({ "root" })
public DefaultTreeModel(TreeNode root) 

Source Link

Document

Creates a tree in which any node can have children.

Usage

From source file:MainClass.java

public void init() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
    DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
    DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");

    treeModel = new DefaultTreeModel(root);
    tree = new JTree(treeModel);

    treeModel.insertNodeInto(subroot, root, 0);
    subroot.add(leaf1);/* ww w .j ava 2s . com*/
    root.add(leaf2);

    tree.putClientProperty("JTree.lineStyle", "Angled");
    getContentPane().add(tree, BorderLayout.CENTER);

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    renderer.setClosedIcon(new ImageIcon("door.closed.gif"));
    renderer.setOpenIcon(new ImageIcon("door.open.gif"));
    renderer.setLeafIcon(new ImageIcon("world.gif"));

}

From source file:StAXStreamTreeViewer.java

public StAXStreamTreeViewer() throws Exception {
    StAXStreamTreeViewer viewer = new StAXStreamTreeViewer();
    File file = new File("yourXML.xml");
    DefaultMutableTreeNode base = new DefaultMutableTreeNode("XML Document: " + file.getAbsolutePath());

    defaultTreeModel = new DefaultTreeModel(base);
    jTree = new JTree(defaultTreeModel);

    buildTree(defaultTreeModel, base, file);

    getContentPane().add(new JScrollPane(jTree), BorderLayout.CENTER);
    viewer.setVisible(true);//from w  w  w  .j  a  v  a 2 s.  co m
    setSize(800, 450);
}

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 ww w  . ja v  a2s  . c om*/
    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:AncestorTree.java

public AncestorTree() {
    super("Ancestor Tree");
    setSize(400, 300);/*from  ww  w .  j  a  va  2s.  co  m*/

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

public void init() {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
    DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
    DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");

    treeModel = new DefaultTreeModel(root);
    tree = new JTree(treeModel);

    treeModel.insertNodeInto(subroot, root, 0);
    subroot.add(leaf1);//from   www .j a  v  a  2 s.c  o  m
    root.add(leaf2);

    tree.putClientProperty("JTree.lineStyle", "Angled");
    getContentPane().add(tree, BorderLayout.CENTER);
}

From source file:VSX.java

public TreeModel parse(String filename) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    XMLTreeHandler handler = new XMLTreeHandler();
    try {//from  w  w w  . jav a  2 s. c om
        // Parse the input.
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(new File(filename), handler);
    } catch (Exception e) {
        System.err.println("File Read Error: " + e);
        e.printStackTrace();
        return new DefaultTreeModel(new DefaultMutableTreeNode("error"));
    }
    return new DefaultTreeModel(handler.getRoot());
}

From source file:UIDefaultsTreeModel.java

public UIDefaultsTreeModel() {
    innerModel = new DefaultTreeModel(rootNode);
    innerModel.insertNodeInto(colorNode, rootNode, 0);
    innerModel.insertNodeInto(borderNode, rootNode, 1);
    innerModel.insertNodeInto(fontNode, rootNode, 2);
    innerModel.insertNodeInto(iconNode, rootNode, 3);
    innerModel.insertNodeInto(otherNode, rootNode, 4);
    UIDefaults defaults = UIManager.getDefaults();
    Enumeration elems = defaults.keys();
    String keyName;//from  w  w  w . j  av a2s. c  o m
    Object valueForKey;
    while (elems.hasMoreElements()) {
        DefaultMutableTreeNode newKeyNode;
        DefaultMutableTreeNode newValueNode;
        try {
            keyName = elems.nextElement().toString();
            valueForKey = defaults.get(keyName);

            newKeyNode = new DefaultMutableTreeNode(keyName);
            newValueNode = new DefaultMutableTreeNode(valueForKey);

            if (valueForKey instanceof java.awt.Color) {
                innerModel.insertNodeInto(newKeyNode, colorNode, 0);
            } else if (valueForKey instanceof javax.swing.border.Border) {
                innerModel.insertNodeInto(newKeyNode, borderNode, 0);
            } else if (valueForKey instanceof java.awt.Font) {
                innerModel.insertNodeInto(newKeyNode, fontNode, 0);
            } else if (valueForKey instanceof javax.swing.Icon) {
                innerModel.insertNodeInto(newKeyNode, iconNode, 0);
            } else {
                innerModel.insertNodeInto(newKeyNode, otherNode, 0);
            }
            innerModel.insertNodeInto(newValueNode, newKeyNode, 0);
        } catch (NullPointerException e) {
        }
    }
}

From source file:TestTree.java

public void init() {
    // Build up a bunch of TreeNodes. We use DefaultMutableTreeNode because
    // the//from  www  .  j  av  a  2 s  .co  m
    // 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: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 . ja v a  2s. 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:ListAlgorithmsBC.java

private ListAlgorithmsBC() {
    super("JCE Algorithms");
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Providers");
    DefaultTreeModel treeModel = new DefaultTreeModel(root);
    for (Provider provider : java.security.Security.getProviders()) {
        DefaultMutableTreeNode providerNode = new DefaultMutableTreeNode(provider);
        root.add(providerNode);/* w w w  . ja v  a  2  s  .c o m*/
        Set<Provider.Service> used = new HashSet<Provider.Service>();

        getNodes(providerNode, provider, used, "Cipher", "cipher");
        getNodes(providerNode, provider, used, "Key Agreement", "keyagreement");
        getNodes(providerNode, provider, used, "Key Generator", "keygenerator");
        getNodes(providerNode, provider, used, "Key Pair Generator", "keypairgenerator");
        getNodes(providerNode, provider, used, "Key Factory", "keyfactory");
        getNodes(providerNode, provider, used, "Secret Key Factory", "secretkeyfactory");
        getNodes(providerNode, provider, used, "Mac", "mac");
        getNodes(providerNode, provider, used, "Message Digest", "messagedigest");
        getNodes(providerNode, provider, used, "Signature", "signature");
        getNodes(providerNode, provider, used, "Algorithm Paramater", "algorithmparameters");
        getNodes(providerNode, provider, used, "Algorithm Paramater Generator", "algorithmparametergenerator");
        getNodes(providerNode, provider, used, "Key Store", "keystore");
        getNodes(providerNode, provider, used, "Secure Random", "securerandom");
        getNodes(providerNode, provider, used, "Certificate Factory", "certificatefactory");
        getNodes(providerNode, provider, used, "Certificate Store", "certstore");
        getNodes(providerNode, provider, used, "Key Manager Factory", "KeyManagerFactory");
        getNodes(providerNode, provider, used, "Trust Manager Factory", "TrustManagerFactory");
        getNodes(providerNode, provider, used, "SSL Context", "SSLContext");
        getNodes(providerNode, provider, used, "Sasl Server Factory", "SaslServerFactory");
        getNodes(providerNode, provider, used, "Sasl Client Factory", "SaslClientFactory");
        {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode("Other");
            for (Provider.Service service : provider.getServices()) {
                if (!used.contains(service)) {
                    DefaultMutableTreeNode serviceNode = new DefaultMutableTreeNode(
                            service.getType() + " : " + service.getAlgorithm());
                    node.add(serviceNode);
                    serviceNode.add(new DefaultMutableTreeNode("class : " + service.getClassName()));
                }
            }
            if (node.getChildCount() != 0)
                providerNode.add(node);
        }
    }

    JTree tree = new JTree(treeModel);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setEditable(false);
    JScrollPane pane = new JScrollPane(tree);
    pane.setPreferredSize(new Dimension(200, 200));

    getContentPane().add(pane);

    pack();
}