Example usage for javax.swing JTree setEditable

List of usage examples for javax.swing JTree setEditable

Introduction

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

Prototype

@BeanProperty(description = "Whether the tree is editable.")
public void setEditable(boolean flag) 

Source Link

Document

Determines whether the tree is editable.

Usage

From source file:TreeEdit.java

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

    Object array[] = { Boolean.TRUE, Boolean.FALSE, "Hello" };
    JTree tree = new JTree(array);
    tree.setEditable(true);
    tree.setRootVisible(true);/*  ww w.ja  v  a  2  s .  c om*/

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    String elements[] = { "A", "B", "C", "D" };
    JComboBox comboBox = new JComboBox(elements);
    comboBox.setEditable(true);
    TreeCellEditor comboEditor = new DefaultCellEditor(comboBox);
    TreeCellEditor editor = new DefaultTreeCellEditor(tree, renderer, comboEditor);
    tree.setCellEditor(editor);

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

From source file:EditLeafSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Editable Tree");
    JTree tree = new JTree();
    tree.setEditable(true);

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    TreeCellEditor editor = new LeafCellEditor(tree, renderer);
    tree.setCellEditor(editor);/*from  w  ww  .  j a  v a 2s  . co m*/

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

From source file:TreeEditJTextField.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Properties props = System.getProperties();
    JTree tree = new JTree(props);

    JTextField textField = new JTextField();
    TreeCellEditor editor = new DefaultCellEditor(textField);

    tree.setEditable(true);
    tree.setCellEditor(editor);/*ww  w . java 2  s .c o m*/

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

From source file:Main.java

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

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Properties props = System.getProperties();
    JTree tree = new JTree(props);

    JCheckBox checkBox = new JCheckBox("To be or not to be");
    TreeCellEditor editor = new DefaultCellEditor(checkBox);

    tree.setEditable(true);
    tree.setCellEditor(editor);//w  w w . j  a  v  a  2  s  . c om

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

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Properties props = System.getProperties();
    JTree tree = new JTree(props);

    JComboBox comboBox = new JComboBox(new String[] { "A", "B", "C" });
    TreeCellEditor editor = new DefaultCellEditor(comboBox);

    tree.setEditable(true);
    tree.setCellEditor(editor);//from w  ww .ja  va  2  s .  c  o m

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

From source file:CheckBoxNodeTreeSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("CheckBox Tree");

    CheckBoxNode accessibilityOptions[] = {
            new CheckBoxNode("Move system caret with focus/selection changes", false),
            new CheckBoxNode("Always expand alt text for images", true) };
    CheckBoxNode browsingOptions[] = { new CheckBoxNode("Notify when downloads complete", true),
            new CheckBoxNode("Disable script debugging", true), new CheckBoxNode("Use AutoComplete", true),
            new CheckBoxNode("Browse in a new process", false) };
    Vector accessVector = new NamedVector("Accessibility", accessibilityOptions);
    Vector browseVector = new NamedVector("Browsing", browsingOptions);
    Object rootNodes[] = { accessVector, browseVector };
    Vector rootVector = new NamedVector("Root", rootNodes);
    JTree tree = new JTree(rootVector);

    CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();
    tree.setCellRenderer(renderer);//  w w w. ja va 2 s  .co  m

    tree.setCellEditor(new CheckBoxNodeEditor(tree));
    tree.setEditable(true);

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

From source file:Main.java

public static void main(String[] args) {
    String[] m = { "A", "B", "C" };
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(new Node("Values"));
    root.add(new DefaultMutableTreeNode(new Node("Value 1", m)));
    root.add(new DefaultMutableTreeNode(new Node("Value 2", m)));
    DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(new Node("Value 3", m));
    root.add(leaf);//from   w  ww. j ava 2 s . c  o  m
    leaf.add(new DefaultMutableTreeNode(new Node("Value 3A", m)));
    leaf.add(new DefaultMutableTreeNode(new Node("Value 3B", m)));

    JTree tree = new JTree(root);
    RendererDispatcher rendererDispatcher = new RendererDispatcher(new JComboBox<String>());
    RendererDispatcher editorDispatcher = new RendererDispatcher(new JComboBox<String>());
    tree.setCellRenderer(rendererDispatcher);
    tree.setCellEditor(editorDispatcher);
    tree.setEditable(true);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(tree));
    f.setSize(320, 240);
    f.setVisible(true);
}

From source file:Main.java

public static JComponent makeUI() {
    JTree tree = new JTree();
    TreeModel model = tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    Enumeration e = root.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
        Object o = node.getUserObject();
        if (o instanceof String) {
            node.setUserObject(new CheckBoxNode((String) o, false));
        }/*  w  ww. j a  v a  2s  .c om*/
    }
    tree.setEditable(true);
    tree.setCellRenderer(new CheckBoxNodeRenderer());
    tree.setCellEditor(new CheckBoxNodeEditor());
    tree.expandRow(0);
    return new JScrollPane(tree);
}

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);//from ww w  . j  a v a 2s .  c om
        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();
}