Example usage for javax.swing.tree DefaultTreeModel addTreeModelListener

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

Introduction

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

Prototype

public void addTreeModelListener(TreeModelListener l) 

Source Link

Document

Adds a listener for the TreeModelEvent posted after the tree changes.

Usage

From source file:MyTreeModelListener.java

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

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    JTree tree = new JTree(root);

    DefaultTreeModel treeModel = new DefaultTreeModel(root);
    treeModel.addTreeModelListener(new MyTreeModelListener());

    treeModel.insertNodeInto(new DefaultMutableTreeNode("A"), root, 0);

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

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);/*from w ww .ja v a  2s. co m*/
    frame.setVisible(true);

}

From source file:org.languagetool.gui.ConfigurationDialog.java

@NotNull
private DefaultTreeModel getTreeModel(DefaultMutableTreeNode rootNode) {
    DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
    treeModel.addTreeModelListener(new TreeModelListener() {
        @Override/* w  ww.j  a  va  2 s .  c  o  m*/
        public void treeNodesChanged(TreeModelEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getTreePath().getLastPathComponent();
            int index = e.getChildIndices()[0];
            node = (DefaultMutableTreeNode) node.getChildAt(index);
            if (node instanceof RuleNode) {
                RuleNode o = (RuleNode) node;
                if (o.getRule().isDefaultOff() || o.getRule().getCategory().isDefaultOff()) {
                    if (o.isEnabled()) {
                        config.getEnabledRuleIds().add(o.getRule().getId());
                        config.getDisabledRuleIds().remove(o.getRule().getId());
                    } else {
                        config.getEnabledRuleIds().remove(o.getRule().getId());
                        config.getDisabledRuleIds().add(o.getRule().getId());
                    }
                } else {
                    if (o.isEnabled()) {
                        config.getDisabledRuleIds().remove(o.getRule().getId());
                    } else {
                        config.getDisabledRuleIds().add(o.getRule().getId());
                    }
                }
            }
            if (node instanceof CategoryNode) {
                CategoryNode o = (CategoryNode) node;
                if (o.getCategory().isDefaultOff()) {
                    if (o.isEnabled()) {
                        config.getDisabledCategoryNames().remove(o.getCategory().getName());
                        config.getEnabledCategoryNames().add(o.getCategory().getName());
                    } else {
                        config.getDisabledCategoryNames().add(o.getCategory().getName());
                        config.getEnabledCategoryNames().remove(o.getCategory().getName());
                    }
                } else {
                    if (o.isEnabled()) {
                        config.getDisabledCategoryNames().remove(o.getCategory().getName());
                    } else {
                        config.getDisabledCategoryNames().add(o.getCategory().getName());
                    }
                }
            }
        }

        @Override
        public void treeNodesInserted(TreeModelEvent e) {
        }

        @Override
        public void treeNodesRemoved(TreeModelEvent e) {
        }

        @Override
        public void treeStructureChanged(TreeModelEvent e) {
        }
    });
    return treeModel;
}