Example usage for javax.swing.event TreeSelectionListener TreeSelectionListener

List of usage examples for javax.swing.event TreeSelectionListener TreeSelectionListener

Introduction

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

Prototype

TreeSelectionListener

Source Link

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    JTree tree = new JTree();

    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent evt) {
            TreePath[] paths = evt.getPaths();

            for (int i = 0; i < paths.length; i++) {
                if (evt.isAddedPath(i)) {
                    System.out.println("This node has been selected");
                } else {
                    System.out.println("This node has been deselected");
                }// w  w  w.  j a v  a 2 s .  c  om
            }
        }
    });
}

From source file:Main.java

public static void main(String args[]) {
    JTree tree = new JTree();
    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {

        @Override//from www  . java  2  s  .  com
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            System.out.println("Min: " + treeSource.getMinSelectionRow());
            System.out.println("Max: " + treeSource.getMaxSelectionRow());
            System.out.println("Lead: " + treeSource.getLeadSelectionRow());
            System.out.println("Row: " + treeSource.getSelectionRows()[0]);
        }
    };
    tree.addTreeSelectionListener(treeSelectionListener);
    JFrame frame = new JFrame();
    frame.add(new JScrollPane(tree));
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:TreeSelectionRow.java

public static void main(String args[]) {
    String title = "JTree Sample";
    JFrame frame = new JFrame(title);
    JTree tree = new JTree();
    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            System.out.println("Min: " + treeSource.getMinSelectionRow());
            System.out.println("Max: " + treeSource.getMaxSelectionRow());
            System.out.println("Lead: " + treeSource.getLeadSelectionRow());
            System.out.println("Row: " + treeSource.getSelectionRows()[0]);
        }//  w ww .  j  a  v a  2s  .co m
    };
    tree.addTreeSelectionListener(treeSelectionListener);

    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[] argv) {
    JTree tree = new JTree();
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    int treeSelectedRows[] = { 3, 1 };
    tree.setSelectionRows(treeSelectedRows);
    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {

        @Override//from  www  .  j  a v  a2 s  .  co  m
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            System.out.println("Min: " + treeSource.getMinSelectionRow());
            System.out.println("Max: " + treeSource.getMaxSelectionRow());
            System.out.println("Lead: " + treeSource.getLeadSelectionRow());
            System.out.println("Row: " + treeSource.getSelectionRows()[0]);
        }
    };
    tree.addTreeSelectionListener(treeSelectionListener);
    JFrame frame = new JFrame("JTree With Multi-Discontiguous selection");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setPreferredSize(new Dimension(380, 320));
    frame.setLocation(150, 150);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) {
    JTree tree = new JTree();
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    int treeSelectedRows[] = { 3, 1 };
    tree.setSelectionRows(treeSelectedRows);
    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {

        @Override//from   ww w.ja v a 2  s.  c  o  m
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            System.out.println("Min: " + treeSource.getMinSelectionRow());
            System.out.println("Max: " + treeSource.getMaxSelectionRow());
            System.out.println("Lead: " + treeSource.getLeadSelectionRow());
            System.out.println("Row: " + treeSource.getSelectionRows()[0]);
        }
    };
    tree.addTreeSelectionListener(treeSelectionListener);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setPreferredSize(new Dimension(380, 320));
    frame.setLocation(150, 150);
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

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

    JTree tree = new JTree();
    tree.setRootVisible(true);//from w w  w .ja v  a2s . c  o m
    TreeModel model = tree.getModel();
    Object rootObject = model.getRoot();
    if ((rootObject != null) && (rootObject instanceof DefaultMutableTreeNode)) {
        DefaultMutableTreeNode r = (DefaultMutableTreeNode) rootObject;
        printDescendents(r);
        Enumeration breadth = r.breadthFirstEnumeration();
        Enumeration depth = r.depthFirstEnumeration();
        Enumeration preOrder = r.preorderEnumeration();
        printEnumeration(breadth, "Breadth");
        printEnumeration(depth, "Depth");
        printEnumeration(preOrder, "Pre");
    }

    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            TreePath path = treeSource.getSelectionPath();
            System.out.println(path);
            System.out.println(path.getPath());
            System.out.println(path.getParentPath());
            System.out.println(((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject());
            System.out.println(path.getPathCount());
        }
    };
    tree.addTreeSelectionListener(treeSelectionListener);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 400);
    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 w w .  j  a  v 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:MainClass.java

public static void main(String[] args) {
    // create a hierarchy of nodes
    MutableTreeNode root = new DefaultMutableTreeNode("A");
    MutableTreeNode bNode = new DefaultMutableTreeNode("B");
    MutableTreeNode cNode = new DefaultMutableTreeNode("C");
    root.insert(bNode, 0);// w  ww.  j  ava  2s. c  om
    root.insert(cNode, 1);
    bNode.insert(new DefaultMutableTreeNode("1"), 0);
    bNode.insert(new DefaultMutableTreeNode("2"), 1);
    cNode.insert(new DefaultMutableTreeNode("1"), 0);
    cNode.insert(new DefaultMutableTreeNode("2"), 1);

    final DefaultTreeModel model = new DefaultTreeModel(root);
    final JTree tree = new JTree(model);

    final JTextField nameField = new JTextField("Z");
    final JButton button = new JButton("Add");
    button.setEnabled(false);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TreePath tp = tree.getSelectionPath();
            MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex = 0;
            if (insertNode.getParent() != null) {
                MutableTreeNode parent = (MutableTreeNode) insertNode.getParent();
                insertIndex = parent.getIndex(insertNode) + 1;
                insertNode = parent;
            }
            MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText());
            model.insertNodeInto(node, insertNode, insertIndex);
        }
    });
    JPanel addPanel = new JPanel(new GridLayout(2, 1));
    addPanel.add(nameField);
    addPanel.add(button);

    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            TreePath tp = e.getNewLeadSelectionPath();
            button.setEnabled(tp != null);
        }
    });

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.getContentPane().add(new JScrollPane(tree));
    frame.getContentPane().add(addPanel, BorderLayout.SOUTH);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) {
    MutableTreeNode root = new DefaultMutableTreeNode("A");
    MutableTreeNode beams = new DefaultMutableTreeNode("B");
    MutableTreeNode gears = new DefaultMutableTreeNode("C");
    root.insert(beams, 0);/*from   w  ww . j  av  a 2s  .c  o  m*/
    root.insert(gears, 1);
    beams.insert(new DefaultMutableTreeNode("4 "), 0);
    beams.insert(new DefaultMutableTreeNode("6 "), 1);
    beams.insert(new DefaultMutableTreeNode("8 "), 2);
    beams.insert(new DefaultMutableTreeNode("12 "), 3);
    gears.insert(new DefaultMutableTreeNode("8t"), 0);
    gears.insert(new DefaultMutableTreeNode("24t"), 1);
    gears.insert(new DefaultMutableTreeNode("40t"), 2);

    final DefaultTreeModel model = new DefaultTreeModel(root);
    final JTree tree = new JTree(model);

    final JTextField nameField = new JTextField("16t");
    final JButton button = new JButton("Add a part");
    button.setEnabled(false);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TreePath tp = tree.getSelectionPath();
            MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent();
            int insertIndex = 0;
            if (insertNode.getParent() != null) {
                MutableTreeNode parent = (MutableTreeNode) insertNode.getParent();
                insertIndex = parent.getIndex(insertNode) + 1;
                insertNode = parent;
            }
            MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText());
            model.insertNodeInto(node, insertNode, insertIndex);
        }
    });
    JPanel addPanel = new JPanel(new GridLayout(2, 1));
    addPanel.add(nameField);
    addPanel.add(button);

    tree.addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            TreePath tp = e.getNewLeadSelectionPath();
            button.setEnabled(tp != null);
        }
    });

    JFrame frame = new JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.getContentPane().add(new JScrollPane(tree));
    frame.getContentPane().add(addPanel, BorderLayout.SOUTH);
    frame.setVisible(true);
}

From source file:ws.moor.bt.grapher.Grapher.java

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Please specify a tab-separated values file");
        System.exit(1);//from ww w  .ja  va  2 s .  c o m
    }
    File file = new File(args[0]);
    final CSVMapCollector collector = new CSVMapCollector(
            new CSVSkipFilter(new CSVInputStream(new FileInputStream(file)), 0 * 1000));

    JFrame window = new JFrame("Grapher");
    window.setSize(1100, 800);
    window.setLayout(new BorderLayout());
    final ChartPanel chartPanel = new ChartPanel(null);

    List<String> possibleNames = collector.getAvailableStreams();
    Collections.sort(possibleNames);
    TreeNode root = convertToTree(possibleNames);

    final JTree tree = new JTree(root);
    tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e) {
            List<String> names = new ArrayList<String>();
            final TreePath[] paths = tree.getSelectionModel().getSelectionPaths();
            if (paths == null) {
                chartPanel.setChart(null);
                return;
            }
            for (TreePath path : paths) {
                Object lastPath = path.getLastPathComponent();
                if (lastPath instanceof DefaultMutableTreeNode) {
                    Object value = ((DefaultMutableTreeNode) lastPath).getUserObject();
                    if (value instanceof NodeValue) {
                        names.add(value.toString());
                    }
                }
            }
            chartPanel.setChart(createChart(collector, names.toArray(new String[names.size()])));
        }
    });
    Font font = tree.getFont();
    tree.setFont(font.deriveFont(10.0f));
    JScrollPane scrollPane = new JScrollPane(tree);

    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.setLeftComponent(scrollPane);
    splitPane.setRightComponent(chartPanel);
    splitPane.setDividerLocation(200);

    window.setContentPane(splitPane);
    window.setVisible(true);
}