Example usage for javax.swing.tree TreeSelectionModel SINGLE_TREE_SELECTION

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

Introduction

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

Prototype

int SINGLE_TREE_SELECTION

To view the source code for javax.swing.tree TreeSelectionModel SINGLE_TREE_SELECTION.

Click Source Link

Document

Selection can only contain one path at a time.

Usage

From source file:TreeSingleSelection.java

public static void main(String[] argv) {
    JTree tree = new JTree();

    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    JFrame frame = new JFrame("Tree single selection");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setSize(380, 320);/*from w  ww .j av  a2s . co m*/
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

From source file:TreeSelectionOption.java

public static void main(String[] argv) {
    JTree tree = new JTree();

    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setSize(380, 320);//from   w w w.  j  a  va 2 s. c  om
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

From source file:UsingTreeSelectionListener.java

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

    JTree tree = new JTree();
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.addTreeSelectionListener(new SelectionListener());

    frame.add(new JScrollPane(tree));

    frame.setSize(300, 200);//from   w ww  .j a v  a 2 s.  co m
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(10, 11, 212, 500);
    frame.getContentPane().add(scrollPane);

    JTree tree = new JTree(addNodes(new File(".")));
    tree.setRootVisible(false);/*from  w  ww  . j av a2s . co m*/
    tree.setShowsRootHandles(true);

    tree.setBorder(new LineBorder(new Color(0, 0, 0)));
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    scrollPane.setViewportView(tree);

    tree.setCellRenderer(new FileTreeCellRenderer());

    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame("JTree Demo");
    f.setSize(260, 240);/* w  ww. j av  a2 s.c om*/
    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:ComponentTree.java

/**
 * This main() method demonstrates the use of the ComponentTree class: it
 * puts a ComponentTree component in a Frame, and uses the ComponentTree to
 * display its own GUI hierarchy. It also adds a TreeSelectionListener to
 * display additional information about each component as it is selected
 *//*from   w ww.  j  a  v a2  s.c  o m*/
public static void main(String[] args) {
    // Create a frame for the demo, and handle window close requests
    JFrame frame = new JFrame("ComponentTree Demo");
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    // Create a scroll pane and a "message line" and add them to the
    // center and bottom of the frame.
    JScrollPane scrollpane = new JScrollPane();
    final JLabel msgline = new JLabel(" ");
    frame.getContentPane().add(scrollpane, BorderLayout.CENTER);
    frame.getContentPane().add(msgline, BorderLayout.SOUTH);

    // Now create the ComponentTree object, specifying the frame as the
    // component whose tree is to be displayed. Also set the tree's font.
    JTree tree = new ComponentTree(frame);
    tree.setFont(new Font("SansSerif", Font.BOLD, 12));

    // Only allow a single item in the tree to be selected at once
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    // Add an event listener for notifications when
    // the tree selection state changes.
    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            // Tree selections are referred to by "path"
            // We only care about the last node in the path
            TreePath path = e.getPath();
            Component c = (Component) path.getLastPathComponent();
            // Now we know what component was selected, so
            // display some information about it in the message line
            if (c.isShowing()) {
                Point p = c.getLocationOnScreen();
                msgline.setText("x: " + p.x + "  y: " + p.y + "  width: " + c.getWidth() + "  height: "
                        + c.getHeight());
            } else {
                msgline.setText("component is not showing");
            }
        }
    });

    // Now that we've set up the tree, add it to the scrollpane
    scrollpane.setViewportView(tree);

    // Finally, set the size of the main window, and pop it up.
    frame.setSize(600, 400);
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    super(new BorderLayout());
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");
    createNodes(top);//from w  ww. j  av  a  2  s.  co m

    model = new DefaultTreeModel(top);
    tree = new JTree(model);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    JScrollPane treeView = new JScrollPane(tree);
    add(treeView);

    btnAdd.addActionListener(e -> {
        TreePath treePath = tree.getSelectionPath();
        if (treePath != null) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            DefaultMutableTreeNode child = new DefaultMutableTreeNode("Child " + (++childCount));
            model.insertNodeInto(child, node, node.getChildCount());
        }
    });
    add(btnAdd, BorderLayout.SOUTH);
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("abcde");
    DefaultMutableTreeNode node = new DefaultMutableTreeNode("1");
    node.add(new DefaultMutableTreeNode("12345"));
    node.add(new DefaultMutableTreeNode("testing"));
    root.add(node);// w  w w.  j av  a2  s . c  om
    root.add(new DefaultMutableTreeNode("1234567890"));

    TreeModel tm = new DefaultTreeModel(root);
    JTree tree = new JTree(tm);
    tree.getSelectionModel().addTreeSelectionListener(new Selector());
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    getContentPane().add(tree, BorderLayout.CENTER);
    pack();
    setVisible(true);
}

From source file:AncestorTree.java

public AncestorTree() {
    super("Ancestor Tree");
    setSize(400, 300);/*from  w  w w .j ava  2 s  . c  om*/

    DefaultMutableTreeNode top = new DefaultMutableTreeNode(new IconData(ICON_SELF, "Myself"));
    addAncestors(top);
    m_model = new DefaultTreeModel(top);
    m_tree = new JTree(m_model);
    m_tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    m_tree.setShowsRootHandles(true);
    m_tree.setEditable(true);

    m_renderer = new IconCellRenderer();
    m_tree.setCellRenderer(m_renderer);
    m_editor = new IconCellEditor(m_tree);
    m_tree.setCellEditor(m_editor);
    m_tree.setInvokesStopCellEditing(true);

    m_tree.addMouseListener(new TreeExpander());

    JScrollPane s = new JScrollPane();
    s.getViewport().add(m_tree);
    getContentPane().add(s, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}

From source file:MainClass.java

public MainClass() {
    this.setSize(225, 325);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel1 = new JPanel();

    DefaultMutableTreeNode root, a, b, c, d, e;

    root = new DefaultMutableTreeNode("A");

    a = makeShow("B", root);
    makeShow("C", a);
    makeShow("D", a);

    b = makeShow("E", root);
    d = makeShow("F", b);
    makeShow("G", d);
    e = makeShow("H", b);
    makeShow("I", e);
    makeShow("J", b);
    makeShow("K", b);

    c = makeShow("L", root);
    makeShow("M", c);
    makeShow("N", c);
    makeShow("O", c);

    tree1 = new JTree(root);
    tree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree1.setVisibleRowCount(12);//from  ww  w  . j a  v a  2 s.  c om
    tree1.addTreeSelectionListener(new TreeListener());

    JScrollPane scroll = new JScrollPane(tree1);
    panel1.add(scroll);

    panel1.add(showName);
    this.add(panel1);
    this.setVisible(true);
}