Java by API/javax.swing.tree/TreePath
Содержание
new TreePath(Object singlePath)
import java.awt.BorderLayout;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Swing Package Hierarchy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Vector rootVector = new TreeVector("A", new String[] { "a" });
JTree tree = new JTree(rootVector);
tree.setRootVisible(true);
TreeModel model = tree.getModel();
model.valueForPathChanged(new TreePath(model.getRoot()), "javax.swing");
((DefaultTreeModel) model).reload();
JScrollPane scrollPane = new JScrollPane(tree);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class TreeVector extends Vector {
String name;
TreeVector(String name) {
this.name = name;
}
TreeVector(String name, Object elements[]) {
this.name = name;
for (int i = 0, n = elements.length; i < n; i++) {
add(elements[i]);
}
}
public String toString() {
return name;
}
}
TreeNode: children()
import java.awt.BorderLayout;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Traverse Tree");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree();
tree.setRootVisible(true);
TreeModel model = tree.getModel();
Object rootObject = model.getRoot();
if ((rootObject != null) && (rootObject instanceof DefaultMutableTreeNode)) {
DefaultMutableTreeNode r = (DefaultMutableTreeNode) rootObject;
printDescendents(r);
Enumeration breadth = r.breadthFirstEnumeration();
Enumeration depth = r.depthFirstEnumeration();
Enumeration preOrder = r.preorderEnumeration();
printEnumeration(breadth, "Breadth");
printEnumeration(depth, "Depth");
printEnumeration(preOrder, "Pre");
}
TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
JTree treeSource = (JTree) treeSelectionEvent.getSource();
TreePath path = treeSource.getSelectionPath();
System.out.println(path);
System.out.println(path.getPath());
System.out.println(path.getParentPath());
System.out.println(((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject());
System.out.println(path.getPathCount());
}
};
tree.addTreeSelectionListener(treeSelectionListener);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 400);
frame.setVisible(true);
}
private static void printEnumeration(Enumeration e, String label) {
System.out.println("-----" + label + "-----");
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
}
public static void printDescendents(TreeNode root) {
System.out.println(root);
Enumeration children = root.children();
if (children != null) {
while (children.hasMoreElements()) {
printDescendents((TreeNode) children.nextElement());
}
}
}
}
TreePath: getLastPathComponent()
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
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.MutableTreeNode;
import javax.swing.tree.TreePath;
public class MainClass extends JPanel {
public static void main(String[] args) {
// create a hierarchy of nodes
MutableTreeNode root = new DefaultMutableTreeNode("A");
MutableTreeNode bNode = new DefaultMutableTreeNode("B");
MutableTreeNode cNode = new DefaultMutableTreeNode("C");
root.insert(bNode, 0);
root.insert(cNode, 1);
bNode.insert(new DefaultMutableTreeNode("1"), 0);
bNode.insert(new DefaultMutableTreeNode("2"), 1);
cNode.insert(new DefaultMutableTreeNode("1"), 0);
cNode.insert(new DefaultMutableTreeNode("2"), 1);
final DefaultTreeModel model = new DefaultTreeModel(root);
final JTree tree = new JTree(model);
final JTextField nameField = new JTextField("Z");
final JButton button = new JButton("Add");
button.setEnabled(false);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TreePath tp = tree.getSelectionPath();
MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent();
int insertIndex = 0;
if (insertNode.getParent() != null) {
MutableTreeNode parent = (MutableTreeNode) insertNode.getParent();
insertIndex = parent.getIndex(insertNode) + 1;
insertNode = parent;
}
MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText());
model.insertNodeInto(node, insertNode, insertIndex);
}
});
JPanel addPanel = new JPanel(new GridLayout(2, 1));
addPanel.add(nameField);
addPanel.add(button);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
TreePath tp = e.getNewLeadSelectionPath();
button.setEnabled(tp != null);
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.getContentPane().add(new JScrollPane(tree));
frame.getContentPane().add(addPanel, BorderLayout.SOUTH);
frame.setVisible(true);
}
}
TreePath: getPathCount()
import java.awt.BorderLayout;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Traverse Tree");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree tree = new JTree();
tree.setRootVisible(true);
TreeModel model = tree.getModel();
Object rootObject = model.getRoot();
if ((rootObject != null) && (rootObject instanceof DefaultMutableTreeNode)) {
DefaultMutableTreeNode r = (DefaultMutableTreeNode) rootObject;
printDescendents(r);
Enumeration breadth = r.breadthFirstEnumeration();
Enumeration depth = r.depthFirstEnumeration();
Enumeration preOrder = r.preorderEnumeration();
printEnumeration(breadth, "Breadth");
printEnumeration(depth, "Depth");
printEnumeration(preOrder, "Pre");
}
TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
JTree treeSource = (JTree) treeSelectionEvent.getSource();
TreePath path = treeSource.getSelectionPath();
System.out.println(path);
System.out.println(path.getPath());
System.out.println(path.getParentPath());
System.out.println(((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject());
System.out.println(path.getPathCount());
}
};
tree.addTreeSelectionListener(treeSelectionListener);
JScrollPane scrollPane = new JScrollPane(tree);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 400);
frame.setVisible(true);
}
private static void printEnumeration(Enumeration e, String label) {
System.out.println("-----" + label + "-----");
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
}
public static void printDescendents(TreeNode root) {
System.out.println(root);
Enumeration children = root.children();
if (children != null) {
while (children.hasMoreElements()) {
printDescendents((TreeNode) children.nextElement());
}
}
}
}
TreePath: pathByAddingChild(Object child)
import java.util.Enumeration;
import javax.swing.JTree;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class Main {
public static void main(String[] argv) throws Exception {
JTree tree = new JTree();
TreePath path = findByName(tree, new String[] { "JTree", "A", "a" });
}
public static TreePath findByName(JTree tree, String[] names) {
TreeNode root = (TreeNode) tree.getModel().getRoot();
return find(tree, new TreePath(root), names, 0);
}
private static TreePath find(JTree tree, TreePath parent, Object[] nodes, int depth) {
TreeNode node = (TreeNode) parent.getLastPathComponent();
Object o = node;
if (o.equals(nodes[depth])) {
if (depth == nodes.length - 1) {
return parent;
}
if (node.getChildCount() >= 0) {
for (Enumeration e = node.children(); e.hasMoreElements();) {
TreeNode n = (TreeNode) e.nextElement();
TreePath path = parent.pathByAddingChild(n);
TreePath result = find(tree, path, nodes, depth + 1);
if (result != null) {
return result;
}
}
}
}
return null;
}
}
TreePath: toString()
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
public class MainClass extends JPanel {
public MainClass() {
final JTree tree;
final JTextField jtf;
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
a.add(a2);
DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
b.add(b3);
tree = new JTree(top);
JScrollPane jsp = new JScrollPane(tree);
add(jsp, BorderLayout.CENTER);
jtf = new JTextField("", 20);
add(jtf, BorderLayout.SOUTH);
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
if (tp != null)
jtf.setText(tp.toString());
else
jtf.setText("");
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}