2011-08-08 12 views
4

JTreeで複数のツリーノードをプログラムで選択する機能はありますか? 複数選択モードを設定しましたjtreeプログラムによる複数選択

すべて私のアプリケーションでは、プログラムによっていくつかのノードを選択できるようにしています。しかし、私はそれをする方法を見つけていない。誰かがこれを解決する方法をアドバイスすることができますか?

おかげ

答えて

6
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() { 
    } 
} 
0

ごJTreeのために使用これを

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