Search code examples
javaswinguser-interfacejtree

jtree programmatic multi selection


Is there an ability to select multiple tree nodes in JTree programmatically? I've set multiselection mode by tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

And all I need - make my application be able to select some nodes programmatically. But I've didn't found the way how to do that. Could anybody give advice how to solve this?

Thanks


Solution

  • import java.awt.Dimension;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.tree.*;
    
    public class TreeWithMultiDiscontiguousSelections {
    
        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
                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);
        }
    
        private TreeWithMultiDiscontiguousSelections() {
        }
    }