Example usage for javax.swing.tree TreeSelectionModel setSelectionMode

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

Introduction

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

Prototype

void setSelectionMode(int mode);

Source Link

Document

Sets the selection model, which must be one of SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.

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  . jav  a2s .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:com.qspin.qtaste.ui.TestCaseTree.java

public TestCaseTree(TestCasePane testCasePn) {
    super();//  www.j av  a2 s.c o m
    mTestCaseTree = this;
    this.setCellRenderer(new TestCaseTreeCellRenderer());

    testCasePane = testCasePn;
    testCasePane.setTestCaseTree(this);
    ToolTipManager.sharedInstance().registerComponent(this);

    FileNode rootFileNode = createRootFileNode();

    TCTreeNode rootNode = new TCTreeNode(rootFileNode, true);
    DefaultTreeModel tm = new DefaultTreeModel(rootNode);
    setModel(tm);
    generateScriptsTree(rootFileNode);
    TCTreeListener listener = new TCTreeListener();
    this.addMouseListener(listener);
    addTreeWillExpandListener(listener);
    addTreeSelectionListener(listener);
    TreeSelectionModel selModel = this.getSelectionModel();
    selModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    // drag drop initialization
    ds = new DragSource();
    dt = new DropTarget();

    ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);
    try {
        dt.setComponent(this);
        dt.addDropTargetListener(this);
    } catch (java.util.TooManyListenersException e) {
        logger.error(e.getMessage());
    }

}

From source file:net.sourceforge.pmd.util.designer.Designer.java

private JComponent createASTPanel() {
    astTreeWidget.setCellRenderer(createNoImageTreeCellRenderer());
    TreeSelectionModel model = astTreeWidget.getSelectionModel();
    model.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    model.addTreeSelectionListener(new SymbolTableListener());
    model.addTreeSelectionListener(new CodeHighlightListener());
    return new JScrollPane(astTreeWidget);
}

From source file:org.feistymeow.dragdrop.dragdrop_tree_test.java

public dragdrop_tree_test(String startPath) {
    super("dragdrop_test");

    // create the tree, configure it to show our hashtable nodes, and put it in
    // a scroll pane.
    larch = new DraggableDroppableTree(startPath);
    DefaultTreeModel treeModel = (DefaultTreeModel) larch.getModel();
    larch.setCellRenderer(new CustomCellRenderer());
    TreeSelectionModel selmod = new DefaultTreeSelectionModel();
    selmod.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    larch.setSelectionModel(selmod);//from  w w  w. j av  a2 s .c o  m
    larch.addTreeSelectionListener(this);
    JScrollPane listScrollPane = new JScrollPane(larch);
    // get the files that live in the specified directory.
    String dirName = startPath + "/"; // make sure we think of it as a
                                      // directory.
    String filelist[] = new File(dirName).list();
    MutableTreeNode root_node = (MutableTreeNode) treeModel.getRoot();
    if (root_node == null) {
        logger.error("something is not right about tree.  has null root.");
        System.exit(1);
    }
    // load up the tree with the files in the directory they passed.
    for (int i = 0; i < filelist.length; i++) {
        String thisFileSt = dirName + filelist[i];
        File thisFile = new File(thisFileSt);
        // skip directories for now.
        if (thisFile.isDirectory())
            continue;
        // skip dot files.
        if (filelist[i].startsWith("."))
            continue;
        try {
            // need to trap exceptions from the URI/URL functions.
            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(makeNode(thisFile.getName(),
                    thisFile.toURI().toURL().toString(), thisFile.getAbsolutePath()));
            treeModel.insertNodeInto(newNode, root_node, root_node.getChildCount());
        } catch (java.net.MalformedURLException e) {
            logger.warn("caught an exception while trying to process path: " + thisFile.getAbsolutePath());
        }
    }

    // set our status bar to have the current path info.
    fileName = new JTextField(50);
    // select the root.
    larch.setSelectionPath(larch.getPathForRow(0));

    // pop out all the nodes.
    larch.expandAll();

    // Create a panel that uses FlowLayout (the default).
    JPanel buttonPane = new JPanel();
    buttonPane.add(fileName);

    Container contentPane = getContentPane();
    contentPane.add(listScrollPane, BorderLayout.CENTER);
    contentPane.add(buttonPane, BorderLayout.NORTH);
}