I am getting the path of a leaf node which has three parents from a tree. Will it be possible to construct the parents of the leafnode in a new tree using that path.
Kindly help me in this issue.Thank you.
Printable View
I am getting the path of a leaf node which has three parents from a tree. Will it be possible to construct the parents of the leafnode in a new tree using that path.
Kindly help me in this issue.Thank you.
Code:import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;
import javax.swing.*;
import javax.swing.tree.*;
public class BuildingNodes implements ActionListener {
JTree left;
JTree right;
public void actionPerformed(ActionEvent e) {
if(left.getSelectionPath() != null) {
moveNode(left, right);
}
if(right.getSelectionPath() != null) {
moveNode(right, left);
}
}
private void moveNode(JTree source, JTree target) {
TreePath sourcePath = source.getSelectionPath();
DefaultMutableTreeNode selectedNode =
(DefaultMutableTreeNode)sourcePath.getLastPathComponent();
// Selected node must be a leaf and must not exist in target.
if(!selectedNode.isLeaf() ||
findNode(selectedNode.toString(), target) != null) {
return;
}
String parentId =
((DefaultMutableTreeNode)selectedNode.getParent()).toString();
DefaultMutableTreeNode targetParent =
findTargetParent(sourcePath, target);
DefaultMutableTreeNode child = null;
// Find index of targetParent node value in sourcePath
// and use the next deeper index for the node build.
int numNodes = sourcePath.getPathCount();
for(int i = 0; i < numNodes; i++) {
Object o = sourcePath.getPathComponent(i);
DefaultMutableTreeNode node = (DefaultMutableTreeNode)o;
if(node.toString().equals(targetParent.toString())) {
// Found parent, use next path component
// index to start building child node.
child = buildNode(i+1, sourcePath);
break;
}
}
// Append the child to the targetParent.
DefaultTreeModel targetModel = (DefaultTreeModel)target.getModel();
targetModel.insertNodeInto(child, targetParent,
targetParent.getChildCount());
// Expand the targetParent path to show child.
expand(target, new TreePath(targetParent.getPath()));
// Remove selectedNode from source.
DefaultTreeModel sourceModel = (DefaultTreeModel)source.getModel();
sourceModel.removeNodeFromParent(selectedNode);
}
/**
* Search target tree for path elements of source tree.
*/
private DefaultMutableTreeNode findTargetParent(TreePath path,
JTree tree) {
DefaultMutableTreeNode root =
(DefaultMutableTreeNode)tree.getModel().getRoot();
// Start at maximum depth in path and look for a
// matching node in tree. Return first match.
int lastIndex = path.getPathCount()-1;
for(int i = lastIndex; i >= 0; i--) {
Object o = path.getPathComponent(i);
String s = ((DefaultMutableTreeNode)o).toString();
DefaultMutableTreeNode targetNode = findNode(s, tree);
if(targetNode == null) {
continue;
}
if(root.isNodeDescendant(targetNode)) { // returns root
return targetNode;
}
}
return null;
}
private DefaultMutableTreeNode findNode(String s, JTree tree) {
DefaultMutableTreeNode root =
(DefaultMutableTreeNode)tree.getModel().getRoot();
Enumeration e = root.breadthFirstEnumeration();
while(e.hasMoreElements()) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)e.nextElement();
if(node.toString().equals(s)) {
return node;
}
}
return null;
}
private DefaultMutableTreeNode buildNode(int index, TreePath path) {
int numNodes = path.getPathCount();
DefaultMutableTreeNode node = null;
DefaultMutableTreeNode lastNode = null;
for(int i = index; i < numNodes; i++) {
Object o = path.getPathComponent(i);
String s = ((DefaultMutableTreeNode)o).toString();
if(i == index) {
node = new DefaultMutableTreeNode(s);
lastNode = node;
} else {
DefaultMutableTreeNode n = new DefaultMutableTreeNode(s);
lastNode.add(n);
lastNode = n;
}
}
return node;
}
private void expand(JTree tree, TreePath path) {
TreeNode node = (TreeNode)path.getLastPathComponent();
if (node.getChildCount() > 0) {
Enumeration e = node.children();
while(e.hasMoreElements()) {
TreeNode n = (TreeNode)e.nextElement();
expand(tree, path.pathByAddingChild(n));
}
}
tree.expandPath(path);
}
private JScrollPane getCenterComponent() {
int[][] keys = { { 1, 3 }, { }, { 2 } };
left = new JTree(new DefaultTreeModel(getRoot(keys)));
expandTree(left);
keys = new int[][] { { 0, 2, 4 }, { 1 } };
right = new JTree(new DefaultTreeModel(getRoot(keys)));
expandTree(right);
JPanel panel = new JPanel(new GridLayout(1,0));
panel.add(new JScrollPane(left));
panel.add(new JScrollPane(right));
return new JScrollPane(panel);
}
private DefaultMutableTreeNode getRoot(int[][] keys) {
DefaultMutableTreeNode root =
new DefaultMutableTreeNode("root");
DefaultMutableTreeNode[] nodes =
new DefaultMutableTreeNode[keys.length];
int n = 0;
for(int i = 0; i < keys.length; i++) {
n += keys[i].length;
}
DefaultMutableTreeNode[] subNodes = new DefaultMutableTreeNode[n];
DefaultMutableTreeNode leaf;
int subIndex = 0;
for(int i = 0; i < nodes.length; i++) {
nodes[i] = new DefaultMutableTreeNode(String.valueOf(i+1));
root.insert(nodes[i], i);
for(int j = 0; j < keys[i].length; j++, subIndex++) {
String s = String.valueOf(i+1) + (j+1);
subNodes[subIndex] =
new DefaultMutableTreeNode(s);
nodes[i].insert(subNodes[subIndex], j);
for(int k = 0; k < keys[i][j]; k++) {
s = String.valueOf(i+1) + (j+1) + (k+1);
leaf = new DefaultMutableTreeNode(s);
subNodes[subIndex].insert(leaf, k);
}
}
}
return root;
}
private void expandTree(JTree tree) {
DefaultMutableTreeNode root =
(DefaultMutableTreeNode)tree.getModel().getRoot();
Enumeration e = root.breadthFirstEnumeration();
while(e.hasMoreElements()) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)e.nextElement();
if(node.isLeaf()) continue;
int row = tree.getRowForPath(new TreePath(node.getPath()));
tree.expandRow(row);
}
}
private JPanel getLastComponent() {
JButton button = new JButton("move");
button.addActionListener(this);
JPanel panel = new JPanel();
panel.add(button);
return panel;
}
public static void main(String[] args) {
BuildingNodes test = new BuildingNodes();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(test.getCenterComponent());
f.add(test.getLastComponent(), "Last");
f.setSize(500,400);
f.setLocation(20,75);
f.setVisible(true);
}
}
Thank you very much for your code. I will get you back if I get any queries over this.
Thank you.