b+trees
View Answers
July 17, 2009 at 3:58 PM
Hi Friend,
Try the following code:
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.text.*;
public class TreeExample{
public static DefaultTreeModel model;
public static TreePath path;
public static JTree tree;
public static DefaultMutableTreeNode nNode;
public static MutableTreeNode node;
String nodeName;
public static void main(String[] args) {
JFrame frame = new JFrame("Adding a Node to a JTree Component!");
JPanel panel = new JPanel();
DefaultMutableTreeNode com = new DefaultMutableTreeNode("My Computer");
DefaultMutableTreeNode c = new DefaultMutableTreeNode("Local Disk(C:)");
DefaultMutableTreeNode ex = new DefaultMutableTreeNode("Examples");
DefaultMutableTreeNode swing = new DefaultMutableTreeNode("Swing");
DefaultMutableTreeNode tr = new DefaultMutableTreeNode("Tree");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("3½ Floppy(A:)");
DefaultMutableTreeNode e = new DefaultMutableTreeNode("New Volume(E:)");
c.add(ex);
ex.add(swing);
swing.add(tr);
com.add(c);
com.add(a);
com.add(e);
tree = new JTree(com);
JButton button = new JButton("Add Node");
JButton button1 = new JButton("Remove Node");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
model = (DefaultTreeModel)tree.getModel();
String nodeName = JOptionPane.showInputDialog(null, "Enter the node name:");
if(nodeName.equals("")){
JOptionPane.showMessageDialog(null, "Node is not added in the tree!");
}
else{
nNode = new DefaultMutableTreeNode(nodeName);
path = tree.getNextMatch("M", 0, Position.Bias.Forward);
node = (MutableTreeNode)path.getLastPathComponent();
model.insertNodeInto(nNode, node, node.getChildCount());
JOptionPane.showMessageDialog(null, "Node are added in the tree!");
}
}
});
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
model = (DefaultTreeModel)tree.getModel();
String nodeName = JOptionPane.showInputDialog(null, "Enter the node name to remove");
if(nodeName.equals("")){
JOptionPane.showMessageDialog(null, "Node could not be deleted from tree!");
}
else{
path = tree.getNextMatch(nodeName, 0, Position.Bias.Forward);
node = (MutableTreeNode)path.getLastPathComponent();
model.removeNodeFromParent(node);
JOptionPane.showMessageDialog(null, "Node is deleted from tree!");
}
}
});
panel.add(tree);
panel.add(button);
panel.add(button1);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Thanks
July 17, 2009 at 3:59 PM
Hi Friend,
Try the following code:
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.text.*;
public class TreeExample{
public static DefaultTreeModel model;
public static TreePath path;
public static JTree tree;
public static DefaultMutableTreeNode nNode;
public static MutableTreeNode node;
String nodeName;
public static void main(String[] args) {
JFrame frame = new JFrame("Adding a Node to a JTree Component!");
JPanel panel = new JPanel();
DefaultMutableTreeNode com = new DefaultMutableTreeNode("My Computer");
DefaultMutableTreeNode c = new DefaultMutableTreeNode("Local Disk(C:)");
DefaultMutableTreeNode ex = new DefaultMutableTreeNode("Examples");
DefaultMutableTreeNode swing = new DefaultMutableTreeNode("Swing");
DefaultMutableTreeNode tr = new DefaultMutableTreeNode("Tree");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("3½ Floppy(A:)");
DefaultMutableTreeNode e = new DefaultMutableTreeNode("New Volume(E:)");
c.add(ex);
ex.add(swing);
swing.add(tr);
com.add(c);
com.add(a);
com.add(e);
tree = new JTree(com);
JButton button = new JButton("Add Node");
JButton button1 = new JButton("Remove Node");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
model = (DefaultTreeModel)tree.getModel();
String nodeName = JOptionPane.showInputDialog(null, "Enter the node name:");
if(nodeName.equals("")){
JOptionPane.showMessageDialog(null, "Node is not added in the tree!");
}
else{
nNode = new DefaultMutableTreeNode(nodeName);
path = tree.getNextMatch("M", 0, Position.Bias.Forward);
node = (MutableTreeNode)path.getLastPathComponent();
model.insertNodeInto(nNode, node, node.getChildCount());
JOptionPane.showMessageDialog(null, "Node are added in the tree!");
}
}
});
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
model = (DefaultTreeModel)tree.getModel();
String nodeName = JOptionPane.showInputDialog(null, "Enter the node name to remove");
if(nodeName.equals("")){
JOptionPane.showMessageDialog(null, "Node could not be deleted from tree!");
}
else{
path = tree.getNextMatch(nodeName, 0, Position.Bias.Forward);
node = (MutableTreeNode)path.getLastPathComponent();
model.removeNodeFromParent(node);
JOptionPane.showMessageDialog(null, "Node is deleted from tree!");
}
}
});
panel.add(tree);
panel.add(button);
panel.add(button1);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setSize(200,200);
frame.setVisible(true);
}
}
Thanks
Related Tutorials/Questions & Answers:
B+ trees searchB+
trees search Can anyone send the code for implementing the
B+
trees searching on a oracle database?
please your answer will be useful for my project
B+ trees searchB+
trees search Can anyone send the code for implementing the
B+
trees searching on a oracle database?
please your answer will be useful for my project
Advertisements
b+trees - UMLb+trees i need use case diagrams,class diagrams and flowcharts for
b+
trees urgently
b+ trees - UMLb+ trees can i get entire documentation of
b+
trees implementation in java
b+trees - Swing AWTb+trees i urgently need source code of
b+
trees in java(swings/frames).it is urgent.i also require its example implemented using any string by inserting and deleting it. Hi Friend,
Try the following code:
import