Example usage for javax.swing.event TreeSelectionEvent getPath

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

Introduction

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

Prototype

public TreePath getPath() 

Source Link

Document

Returns the first path element.

Usage

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 a v 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:TreeIt.java

public TreeIt() {
    JFrame f = new JFrame();
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Calendar");
    DefaultMutableTreeNode months = new DefaultMutableTreeNode("Months");
    root.add(months);//from www  .  j ava2 s.  com
    String monthLabels[] = { "January", "February", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December" };
    for (int i = 0, n = monthLabels.length; i < n; i++)
        months.add(new DefaultMutableTreeNode(monthLabels[i]));
    DefaultMutableTreeNode weeks = new DefaultMutableTreeNode("Weeks");
    root.add(weeks);
    String weekLabels[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    for (int i = 0, n = weekLabels.length; i < n; i++)
        weeks.add(new DefaultMutableTreeNode(weekLabels[i]));
    JTree jt = new JTree(root);
    jt.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            TreePath path = e.getPath();
            System.out.println("Picked: " + path.getLastPathComponent());
            Object elements[] = path.getPath();
            for (int i = 0, n = elements.length; i < n; i++) {
                System.out.print("->" + elements[i]);
            }
            System.out.println();
        }
    });

    DefaultMutableTreeNode lastLeaf = root.getLastLeaf();
    TreePath path = new TreePath(lastLeaf.getPath());
    jt.setSelectionPath(path);

    jt.setCellRenderer(new MyCellRenderer());

    JScrollPane jsp = new JScrollPane(jt);
    Container c = f.getContentPane();
    c.add(jsp, BorderLayout.CENTER);
    f.setSize(250, 250);
    f.show();
}

From source file:Main.java

@Override
public void valueChanged(TreeSelectionEvent event) {
    tree.expandPath(new TreePath(e11.getPath()));
    currentSelectionField.setText(event.getPath().toString());
}

From source file:MainClass.java

public MainClass(File dir) {
    setLayout(new BorderLayout());
    JTree tree = new JTree(addNodes(null, dir));

    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
            System.out.println("You selected " + node);
        }/* w w  w.  j a  v  a  2 s .  co  m*/
    });

    JScrollPane scrollpane = new JScrollPane();
    scrollpane.getViewport().add(tree);
    add(BorderLayout.CENTER, scrollpane);
}

From source file:Main.java

public Main(File dir) {
    setLayout(new BorderLayout());
    JTree tree = new JTree(addNodes(null, projectFile));
    tree.setCellRenderer(new MyTreeCellRenderer());
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
            System.out.println("You selected " + node);
        }/*w  ww . java  2s. co m*/
    });

    JScrollPane scrollpane = new JScrollPane();
    scrollpane.getViewport().add(tree);
    add(scrollpane, BorderLayout.CENTER);
}

From source file:FileTree.java

/** Construct a FileTree */
public FileTree(File dir) {
    setLayout(new BorderLayout());

    // Make a tree list with all the nodes, and make it a JTree
    JTree tree = new JTree(addNodes(null, dir));

    // Add a listener
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
            System.out.println("You selected " + node);
        }//from w  ww  .j  a v  a 2 s. c o m
    });

    // Lastly, put the JTree into a JScrollPane.
    JScrollPane scrollpane = new JScrollPane();
    scrollpane.getViewport().add(tree);
    add(BorderLayout.CENTER, scrollpane);
}

From source file:com.ssn.listener.SSNFacebookAlbumSelectionListener.java

@Override
public void valueChanged(TreeSelectionEvent event) {
    DefaultMutableTreeNode node = treeHelper.getTreeNode(event.getPath());

    if (this.form.getHiveTree() != null) {
        this.form.getHiveTree().clearSelection();
    }//from w ww .  j ava  2s.c  om
    if (this.form.getInstagramTree() != null) {
        this.form.getInstagramTree().clearSelection();
    }

    if (node.isLeaf()) {
        this.form.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        SSNAlbumNode fnode = null;
        try {
            if (((SSNIconData) node.getUserObject()).getObject() instanceof SSNAlbumNode)
                fnode = (SSNAlbumNode) ((SSNIconData) node.getUserObject()).getObject();
        } catch (ClassCastException ee) {
            ee.printStackTrace();
            this.form.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
        String facebookDirPath = SSNHelper.getFacebookPhotosDirPath();
        if (fnode != null)
            fileTree.m_display.setText(facebookDirPath + File.separator + fnode);

        SSNIconData iconData = (SSNIconData) node.getUserObject();
        SSNAlbumNode albumNode = (SSNAlbumNode) iconData.getObject();

        MediaOperations mediaOperations = facebook.mediaOperations();

        List<Photo> listPhoto, completePhotoList = new ArrayList<Photo>();
        do {
            PagingParameters pagingParameters = new PagingParameters(100, completePhotoList.size(), null,
                    Calendar.getInstance().getTimeInMillis());
            listPhoto = mediaOperations.getPhotos(albumNode.getAlbum().getId(), pagingParameters);
            completePhotoList.addAll(listPhoto);

        } while (listPhoto.size() > 0);

        createComponents(this.form, completePhotoList, albumNode);
    }

}

From source file:com.ssn.listener.SSNInstagramSelectionListener.java

@Override
public void valueChanged(TreeSelectionEvent event) {
    DefaultMutableTreeNode node = treeHelper.getTreeNode(event.getPath());

    if (this.form.getHiveTree() != null) {
        this.form.getHiveTree().clearSelection();
    }//from www.j a  va2s.  c  o  m

    if (this.form.getFacebookTree() != null) {
        this.form.getFacebookTree().clearSelection();
    }

    if (node.isLeaf()) {
        try {
            this.form.setCursor(new Cursor(Cursor.WAIT_CURSOR));
            String instagramDirPath = SSNHelper.getInstagramPhotosDirPath();
            fileTree.m_display.setText("instagramMedia");
            String urlString = String.format(
                    "https://api.instagram.com/v1/users/self/media/recent/?access_token=%s",
                    accessGrant.getAccessToken());

            List<InstagramMedia> imageList = new ArrayList<>();
            getMedia(urlString, imageList);

            createComponents(this.form, imageList);
        } catch (Exception e) {

        }
    }

}

From source file:org.freeinternals.javaclassviewer.ui.JSplitPaneClassFile.java

private void jTreeClassFileSelectionChanged(final TreeSelectionEvent evt) {
    Object obj = evt.getPath().getLastPathComponent();
    if (obj instanceof DefaultMutableTreeNode) {
        final DefaultMutableTreeNode objDmtn = (DefaultMutableTreeNode) obj;
        obj = objDmtn.getUserObject();/*from   w  w  w  . j  ava2s. c  o m*/
        if (obj instanceof JTreeNodeClassComponent) {
            final JTreeNodeClassComponent objTncc = (JTreeNodeClassComponent) obj;

            // Select the bytes of this data
            this.binaryViewer.setSelection(objTncc.getStartPos(), objTncc.getLength());

            // clear opcode values;
            this.opcode.setText(null);
            // Get the code bytes
            if (objTncc.getText().equals("code")) {
                // System.out.println("code");
                final byte[] data = this.classFile.getClassByteArray(objTncc.getStartPos(),
                        objTncc.getLength());
                this.generateOpcodeParseResult(data);
            }
        }
    }
}

From source file:TreeUtil.java

public void valueChanged(TreeSelectionEvent e) {
    TreePath path = e.getPath();
    Object[] items = path.getPath();
    if (items.length < 1) {
        return;//  w w w  . ja  v a  2 s  . c o m
    }
    String cmd = "";
    for (int i = 0; i < items.length; i++) {
        cmd = cmd + "." + items[i].toString();
    }
    if (cmd.length() > 0) {
        cmd = cmd.substring(1);
    }
    fireActionEvent(new ActionEvent(this, TREE, cmd));
}