File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPopup and JTree Problem" Watch "JPopup and JTree Problem" New topic

JPopup and JTree Problem

M. Imran

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,

import javax.swing.tree.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class MainFrame extends JFrame{
Container contentPane ;
MainFrame mm;
TreePro treePro;
JTree tree;
popup tpopup;
contentPane =getContentPane();
contentPane.setLayout(new BorderLayout());
treePro=new TreePro();
tpopup=new popup(tree);
//JTree jTree=new JTree();
JPanel treePanel=new JPanel();
treePanel.setLayout(new BorderLayout());
treePanel.add(new JScrollPane(tree));
treePanel.setPreferredSize(new Dimension(200, 100));
contentPane.add(treePanel, BorderLayout.WEST);
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");
tree= new JTree(treeModel);
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)
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,
// 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)
try {
int index = e.getChildIndices()[0];
node = (DefaultMutableTreeNode)
} 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) {
popupMenu=new JPopupMenu();
treePro=new TreePro();
item1= new JRadioButtonMenuItem("Add");
item2= new JRadioButtonMenuItem("Remove");
item3= new JRadioButtonMenuItem("Properties");
ItemHandler handler=new ItemHandler();
public JPopupMenu showPopup(){
new MouseAdapter(){
public void mousePressed(MouseEvent e){
public void mouseReleased (MouseEvent e){
private void checkForTriggerEvent (MouseEvent e){
if (e.isPopupTrigger()), e.getX(), e.getY());
}//end of MouseAdapter
return popupMenu;
class ItemHandler implements ActionListener {
public void actionPerformed (ActionEvent e){
System.out.println("From Item Handler");
Nathan Pruett

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:

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

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!
I agree. Here's the link:
subject: JPopup and JTree Problem
jQuery in Action, 3rd edition