This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JPopup and JTree Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPopup and JTree Problem" Watch "JPopup and JTree Problem" New topic
Author

JPopup and JTree Problem

M. Imran
Greenhorn

Joined: Feb 12, 2002
Posts: 21
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");
}
}
}
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The problem is in your popup class...

You shouldn't be passing a JTree in, you should be passing TreePro...

A new treepro instance is getting created in the popup constructor, so when you add, you are adding to the invisible TreePro constructed there...

Here's a fixed popup class:


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
M. Imran
Greenhorn

Joined: Feb 12, 2002
Posts: 21
Thank you ver much for your kind help. Actually, not only me most of us are very found of constructing new objects.
Can you tell us more about this!
 
Consider Paul's rocket mass heater.
 
subject: JPopup and JTree Problem
 
Similar Threads
Gett8ing focus when 2 JTrees in same panel.
I am lost , Help Me Please ...JTree
Jtree - message becomes blurry when scrolls
how to fill the jtree with the last modified value ?
How to recursively traverse JTree?