• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JPopup and JTree Problem

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
}
}
}
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
M. Imran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic