Example usage for javax.swing.event TreeExpansionEvent getPath

List of usage examples for javax.swing.event TreeExpansionEvent getPath

Introduction

In this page you can find the example usage for javax.swing.event TreeExpansionEvent getPath.

Prototype

public TreePath getPath() 

Source Link

Document

Returns the path to the value that has been expanded/collapsed.

Usage

From source file:TreeExpansionListenerDemo.java

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

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    root.add(new DefaultMutableTreeNode("A"));
    root.add(new DefaultMutableTreeNode("B"));
    root.add(new DefaultMutableTreeNode("C"));
    JTree tree = new JTree(root);

    TreeExpansionListener treeExpandListener = new TreeExpansionListener() {

        public void treeExpanded(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            System.out.println("Expanded: " + data);
        }//from   ww w.ja va 2 s .c o  m

        public void treeCollapsed(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            System.out.println("Collapsed: " + data);
        }
    };

    tree.addTreeExpansionListener(treeExpandListener);

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

}

From source file:TreeExpandSample.java

public static void main(String args[]) {
    String title = ("JTree Expand Sample");
    JFrame frame = new JFrame(title);
    JTree tree = new JTree();
    TreeWillExpandListener treeWillExpandListener = new TreeWillExpandListener() {
        public void treeWillCollapse(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {
            TreePath path = treeExpansionEvent.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            if (data.equals("colors")) {
                throw new ExpandVetoException(treeExpansionEvent);
            }/*from  w  w  w.  j  ava2  s  .c o m*/
        }

        public void treeWillExpand(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {
            TreePath path = treeExpansionEvent.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            if (data.equals("sports")) {
                throw new ExpandVetoException(treeExpansionEvent);
            }
        }
    };
    tree.addTreeWillExpandListener(treeWillExpandListener);
    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[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    root.add(new DefaultMutableTreeNode("A"));
    root.add(new DefaultMutableTreeNode("B"));
    root.add(new DefaultMutableTreeNode("C"));
    JTree tree = new JTree(root);

    TreeWillExpandListener treeWillExpandListener = new TreeWillExpandListener() {
        public void treeWillCollapse(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {
            TreePath path = treeExpansionEvent.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            System.out.println("WillCollapse: " + data);

        }/*from w  ww .  ja  v a  2s  .  co  m*/

        public void treeWillExpand(TreeExpansionEvent treeExpansionEvent) throws ExpandVetoException {
            TreePath path = treeExpansionEvent.getPath();
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
            String data = node.getUserObject().toString();
            System.out.println("WillExpand: " + data);

        }
    };

    tree.addTreeWillExpandListener(treeWillExpandListener);

    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 ww  . j  a va2s  .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:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyTreeNode root = new MyTreeNode(1, 0);
    DefaultTreeModel model = new DefaultTreeModel(root);
    JProgressBar bar = new JProgressBar();
    PropertyChangeListener progressListener = new PropertyChangeListener() {
        @Override/*  w  w w. j a  v a 2  s . com*/
        public void propertyChange(PropertyChangeEvent evt) {
            bar.setValue((Integer) evt.getNewValue());
        }
    };
    JTree tree = new JTree();
    tree.setShowsRootHandles(true);
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {
        @Override
        public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
            TreePath path = event.getPath();
            if (path.getLastPathComponent() instanceof MyTreeNode) {
                MyTreeNode node = (MyTreeNode) path.getLastPathComponent();
                node.loadChildren(model, progressListener);
            }
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {

        }
    });
    tree.setModel(model);
    root.loadChildren(model, progressListener);
    frame.add(new JScrollPane(tree));
    frame.add(bar, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public void treeExpanded(TreeExpansionEvent evt) {
    JTree tree = (JTree) evt.getSource();

    TreePath path = evt.getPath();
    System.out.println("treeExpanded");
}

From source file:Main.java

public void treeCollapsed(TreeExpansionEvent evt) {
    JTree tree = (JTree) evt.getSource();

    TreePath path = evt.getPath();
    System.out.println("treeCollapsed");
}

From source file:Main.java

public void treeWillExpand(TreeExpansionEvent evt) throws ExpandVetoException {
    JTree tree = (JTree) evt.getSource();

    TreePath path = evt.getPath();
    boolean veto = false;
    if (veto) {/* w w w. ja  va 2s  .com*/
        throw new ExpandVetoException(evt);
    }
}

From source file:Main.java

public void treeWillCollapse(TreeExpansionEvent evt) throws ExpandVetoException {
    JTree tree = (JTree) evt.getSource();

    TreePath path = evt.getPath();
    boolean veto = false;
    if (veto) {//from w w w.j  ava2 s.co m
        throw new ExpandVetoException(evt);
    }
}

From source file:TreeExpandEventDemo2.java

void saySomething(String eventDescription, TreeExpansionEvent e) {
    textArea.append(eventDescription + "; " + "path = " + e.getPath() + newline);
}