Java Tutorial/Swing/JTree Selection
Содержание
- 1 Allow multiple selections of visible nodes
- 2 Allow only a single node to be selected (default)
- 3 Allow selection to span one vertical contiguous set of visible nodes
- 4 Determine if the third item is selected
- 5 Enabling and Disabling Multiple Selections in a JTree Component
- 6 Getting the Selected Nodes in a JTree Component
- 7 Listening for Selection Events in a JTree Component
- 8 Returns a TreePath containing the specified node.
Allow multiple selections of visible nodes
<source lang="java">
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); }
}</source>
Allow only a single node to be selected (default)
<source lang="java">
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); }
}</source>
Allow selection to span one vertical contiguous set of visible nodes
<source lang="java">
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); }
}</source>
Determine if the third item is selected
<source lang="java">
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); }
}</source>
Enabling and Disabling Multiple Selections in a JTree Component
<source lang="java">
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); }
}</source>
Getting the Selected Nodes in a JTree Component
<source lang="java">
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(); }
}</source>
Listening for Selection Events in a JTree Component
<source lang="java">
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"); } } } }); }
}</source>
Returns a TreePath containing the specified node.
<source lang="java">
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()); }
}</source>