Example usage for javax.swing JTree getSelectionModel

List of usage examples for javax.swing JTree getSelectionModel

Introduction

In this page you can find the example usage for javax.swing JTree getSelectionModel.

Prototype

public TreeSelectionModel getSelectionModel() 

Source Link

Document

Returns the model for selections.

Usage

From source file:TreeDISCONTIGUOUSSelection.java

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

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

    JFrame frame = new JFrame("tree DISCONTIGUOUS selection");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setSize(380, 320);//  w w w.ja va2 s.  com
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

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 w w .  java  2s  .  c  o  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);//  w  w w .j  a  va2  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);// w  ww.j a  v  a2 s. c  o  m
    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   w w w  .jav  a2s.  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  w w w .ja va 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: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);/*  w  w  w  . j  av  a  2s .co  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);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame("JTree Demo");
    f.setSize(260, 240);/*from w  ww . jav a2  s .com*/
    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: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);/* w  w  w  .j a  v  a2 s .  c o 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: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
 */// w w w . j  av  a 2 s .co  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);
}