Java Tutorial/Swing/JTree Selection

Материал из Java эксперт
Перейти к: навигация, поиск

Allow multiple selections of visible nodes

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeSelectionModel;
public class TreeDISCONTIGUOUSSelection {
  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);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





Allow only a single node to be selected (default)

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeSelectionModel;

public class TreeSingleSelection {
  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);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





Allow selection to span one vertical contiguous set of visible nodes

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeSelectionModel;
public class TreeCONTIGUOUSSelection {
  public static void main(String[] argv) {
    JTree tree = new JTree();
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
    JFrame frame = new JFrame("Tree CONTIGUOUS selection");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setSize(380, 320);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





Determine if the third item is selected

import javax.swing.JList;
public class Main {
  public static void main(String[] argv) throws Exception {
    String[] items = { "A", "B", "C", "D" };
    JList list = new JList(items);
    int index = 2;
    boolean isSel = list.isSelectedIndex(index);
  }
}





Enabling and Disabling Multiple Selections in a JTree Component

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeSelectionModel;
public class TreeSelectionOption {
  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);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}





Getting the Selected Nodes in a JTree Component

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreePath;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTree tree = new JTree();
    JFrame f = new JFrame();
    f.add(new JScrollPane(tree));
    f.setSize(300, 300);
    f.setVisible(true);
    // Get paths of all selected nodes
    TreePath[] paths = tree.getSelectionPaths();
  }
}





Listening for Selection Events in a JTree Component

import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
public class Main {
  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"); 
          }
        }
      }
    });
  }
}





Returns a TreePath containing the specified node.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class Main {
 
  public TreePath getPath(TreeNode node) {
    List<TreeNode> list = new ArrayList<TreeNode>();
    while (node != null) {
      list.add(node);
      node = node.getParent();
    }
    Collections.reverse(list);
    return new TreePath(list.toArray());
  }
}