Example usage for javax.swing.tree TreeSelectionModel setSelectionPath

List of usage examples for javax.swing.tree TreeSelectionModel setSelectionPath

Introduction

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

Prototype

void setSelectionPath(TreePath path);

Source Link

Document

Sets the selection to path.

Usage

From source file:com.haulmont.cuba.desktop.gui.components.DesktopTree.java

public DesktopTree() {
    layout = new MigLayout("flowy, fill, insets 0", "", "[min!][fill]");
    panel = new JPanel(layout);

    topPanel = new JPanel(new BorderLayout());
    topPanel.setVisible(false);//from w ww  .j  av  a  2 s .  co m
    panel.add(topPanel, "growx");

    impl = new JTree();
    treeView = new JScrollPane(impl);
    panel.add(treeView, "grow");

    impl.setRootVisible(false);
    impl.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    impl.setExpandsSelectedPaths(true);

    impl.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            showPopup(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            showPopup(e);
        }

        private void showPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                // select row
                Point p = e.getPoint();
                TreePath treePath = impl.getPathForLocation(p.x, p.y);
                if (treePath != null) {
                    TreeSelectionModel model = impl.getSelectionModel();
                    model.setSelectionPath(treePath);
                }
                // show popup menu
                createPopupMenu().show(e.getComponent(), e.getX(), e.getY());
            }
        }
    });

    impl.addKeyListener(new KeyAdapter() {
        protected static final int ENTER_CODE = 10;

        @Override
        public void keyPressed(KeyEvent e) {
            if (ENTER_CODE == e.getKeyCode() && e.getComponent() == DesktopTree.this.getComponent()) {
                if (enterPressAction != null) {
                    enterPressAction.actionPerform(DesktopTree.this);
                } else {
                    handleClickAction();
                }
            }
        }
    });
}

From source file:com.nbt.TreeFrame.java

private void selectAndScroll(TreePath path) {
    if (path == null) {
        System.err.println("no path found");
        return;/*from w  ww . j a v a2 s .  co  m*/
    }

    TreeSelectionModel model = treeTable.getTreeSelectionModel();
    model.setSelectionPath(path);

    treeTable.expandPath(path);
    treeTable.scrollPathToVisible(path);
}