Dear All,
I am here with a very irritating problem. I have constructed an application with popup menu and a tree. Purpose of this application is to add a JTreeNode with the help of Popup menu.
Its all methods and listeners are working fine but they are unable to add a new tree node. This will be a great help on my part as well for others like me to sort this problem. Code is under:
Thank you,
Imran
import javax.swing.tree.*;
import java.io.IOException;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MainFrame extends JFrame{
Container contentPane ;
MainFrame mm;
TreePro treePro;
JTree tree;
popup tpopup;
MainFrame(){
contentPane =getContentPane();
contentPane.setLayout(new BorderLayout());
treePro=new TreePro();
tree=treePro.getJTree();
tpopup=new popup(tree);
//JTree jTree=new JTree();
tpopup.showPopup();
JPanel treePanel=new JPanel();
treePanel.setLayout(new BorderLayout());
treePanel.add(new JScrollPane(tree));
treePanel.repaint();
treePanel.setPreferredSize(new Dimension(200, 100));
contentPane.add(treePanel, BorderLayout.WEST);
setSize(800,800);
show();
}
public static void main (
String args[]){
MainFrame mm= new MainFrame();
}
}
///////////////////////////////////////////////////////////////////////////
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TreePro {
JTree jtree;
DefaultMutableTreeNode p1, p2, p3,rootNode;
DefaultTreeModel treeModel;
popup tpopup;
JTree tree;
JPanel treePanel;
public TreePro() {
rootNode = new DefaultMutableTreeNode("Main");
treeModel=new DefaultTreeModel(rootNode);
treeModel.addTreeModelListener(new MyTreeModelListener());
p1=new DefaultMutableTreeNode("P1");
p2=new DefaultMutableTreeNode("P2");
p3=new DefaultMutableTreeNode("P3");
rootNode.add(p1);
rootNode.add(p2);
tree= new JTree(treeModel);
tree.setEditable(true);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
}
JTree getJTree(){
return tree;
}
public DefaultMutableTreeNode addObject(Object child) {
DefaultMutableTreeNode parentNode = null;
TreePath parentPath = tree.getSelectionPath();
System.out.println("The from user object 1");
if (parentPath == null) {
parentNode = rootNode;
} else {
parentNode = (DefaultMutableTreeNode)
(parentPath.getLastPathComponent());
}
return addObject(parentNode, child, true);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
Object child) {
return addObject(parent, child, false);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
Object child,
boolean shouldBeVisible) {
DefaultMutableTreeNode childNode =
new DefaultMutableTreeNode(child);
if (parent == null) {
parent = rootNode;
}
treeModel.insertNodeInto(childNode, parent,
parent.getChildCount());
// Make sure the user can see the lovely new node.
if (shouldBeVisible) {
tree.scrollPathToVisible(new TreePath(childNode.getPath()));
}
return childNode;
}
class MyTreeModelListener implements TreeModelListener {
public void treeNodesChanged(TreeModelEvent e) {
DefaultMutableTreeNode node;
node = (DefaultMutableTreeNode)
(e.getTreePath().getLastPathComponent());
try {
int index = e.getChildIndices()[0];
node = (DefaultMutableTreeNode)
(node.getChildAt(index));
} catch (NullPointerException exc) {
System.out.println("Some thing happen bad");
}
System.out.println("The user has finished editing the node.");
System.out.println("New value: " + node.getUserObject());
}
public void treeNodesInserted(TreeModelEvent e) {
System.out.println("treeNode Inserted");
}
public void treeNodesRemoved(TreeModelEvent e) {
}
public void treeStructureChanged(TreeModelEvent e) {
}
}
}//class TreePro
/////////////////////////////////////////////////////////////
//class which Make popup Menu
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.*;
public class popup{
JRadioButtonMenuItem item1, item2, item3;
JPopupMenu popupMenu;
JTree jtree;
DefaultMutableTreeNode muTableNode;
MainFrame mainFrame;
TreePro treePro;
public popup(JTree jtreee) {
jtree=jtreee;
popupMenu=new JPopupMenu();
treePro=new TreePro();
item1= new JRadioButtonMenuItem("Add");
item2= new JRadioButtonMenuItem("Remove");
item3= new JRadioButtonMenuItem("Properties");
ItemHandler handler=new ItemHandler();
item1.addActionListener(handler);
popupMenu.add(item1);
popupMenu.add(item2);
popupMenu.add(item3);
}
public JPopupMenu showPopup(){
jtree.addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e){
checkForTriggerEvent(e);
}
public void mouseReleased (MouseEvent e){
checkForTriggerEvent(e);
}
private void checkForTriggerEvent (MouseEvent e){
if (e.isPopupTrigger())
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}//end of MouseAdapter
);
return popupMenu;
}
class ItemHandler implements ActionListener {
public void actionPerformed (ActionEvent e){
treePro.addObject("q");
System.out.println("From Item Handler");
}
}
}