Java by API/javax.swing.tree/TreeSelectionModel

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

TreeSelectionModel: addTreeSelectionListener(TreeSelectionListener x)

  
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeSelectionModel;
public class Main extends JFrame {
  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);
    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);
  }
  private class Selector implements TreeSelectionListener {
    public void valueChanged(TreeSelectionEvent event) {
      Object obj = event.getNewLeadSelectionPath().getLastPathComponent();
      System.out.println("" + obj.toString().length());
    }
  }
  public static void main(String[] args) {
    Main m = new Main();
  }
}





TreeSelectionModel.CONTIGUOUS_TREE_SELECTION

  
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeSelectionModel;
public class Main {
  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);
  }
}





TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION

  
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeSelectionModel;
public class Main {
  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);
  }
}





TreeSelectionModel: setSelectionMode(int mode)

  
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeSelectionModel;
public class Main extends JFrame {
  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);
    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);
  }
  private class Selector implements TreeSelectionListener {
    public void valueChanged(TreeSelectionEvent event) {
      Object obj = event.getNewLeadSelectionPath().getLastPathComponent();
      System.out.println("" + obj.toString().length());
    }
  }
  public static void main(String[] args) {
    Main m = new Main();
  }
}





TreeSelectionModel.SINGLE_TREE_SELECTION

  
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeSelectionModel;
public class Main {
  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);
  }
}