2014-03-27 134 views
0

我想動態地將節點添加到彈出組件和JMenuItem組件的單擊操作事件中的JTree。我右鍵單擊節點組件並添加另一個節點。節點的添加對葉節點起作用,但對非葉節點不起作用。我不知道代碼的問題是什麼。看起來好像添加了節點,但Gui沒有更新。節點動態添加到非葉節點不工作| JTree

平臺: Windows 7的

的Java Java版本 「1.7.0_17」 的Java(TM)SE運行時環境(建立1.7.0_17-B02) 的HotSpot的Java(TM)64位服務器VM(構建23.7-b01,混合模式)

package test; 

/** 
* 
* @author Rajan 
*/ 

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JPopupMenu; 
import javax.swing.JScrollPane; 
import javax.swing.JSplitPane; 
import javax.swing.JTree; 
import javax.swing.tree.DefaultMutableTreeNode; 
import javax.swing.tree.DefaultTreeModel; 
import javax.swing.tree.MutableTreeNode; 
import javax.swing.tree.TreeNode; 
import javax.swing.tree.TreePath; 

/** 
* 
* @author Rajan Prasad Upadhyay 
*/ 
public class TreeTest { 

    String name = "name"; 

    public static void main(String[] args) { 
     javax.swing.JFrame topFrame = new JFrame(); 
     javax.swing.JPanel topPanel = new JPanel(); 
     javax.swing.JScrollPane leftScrollPane = new JScrollPane(); 
     javax.swing.JTree leftTree = new JTree(); 

     topFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     //layouts 
     topFrame.setLayout(new BorderLayout()); 
     topPanel.setLayout(new BorderLayout()); 

     // addition of components 
//  topPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE)); 
     topPanel.add(leftScrollPane); 

     leftScrollPane.getViewport().add(leftTree); 

     addExperimentEvents(leftTree); 

     topFrame.setSize(600, 500); 
     topFrame.setContentPane(topPanel); 
     topFrame.setVisible(true); 
    } 

    public static void addExperimentEvents(JTree tree) { 
     MouseAdapter ma = new MouseAdapter() { 
      private void myPopupEvent(MouseEvent e) { 
       int x = e.getX(); 
       int y = e.getY(); 
       final JTree tree = (JTree) e.getSource(); 
       TreePath path = tree.getPathForLocation(x, y); 
       if (path == null) { 
        return; 
       } 

       tree.setSelectionPath(path); 

       Object obj = (Object) path.getLastPathComponent(); 

       String label = "Add:";// + obj.toString(); 
       JPopupMenu popup = new JPopupMenu("Menu"); 

       JMenu sectionsMenu = new JMenu("Sections"); 
       JMenuItem menuItem1 = new JMenuItem("Item1"); 
       sectionsMenu.add(menuItem1); 
       JMenuItem menuItem2 = new JMenuItem("Item2"); 
       sectionsMenu.add(menuItem2); 

       JMenuItem item = new JMenuItem(label); 
       item.addActionListener(new ActionListener() { 

        @Override 
        public void actionPerformed(ActionEvent ae) { 
         System.out.println("Editing the tree"); 
         DefaultMutableTreeNode selNode = (DefaultMutableTreeNode) tree 
           .getLastSelectedPathComponent(); 
         TreeNode root = (TreeNode) tree.getModel().getRoot(); 
         DefaultTreeModel model = new DefaultTreeModel(root); 

         if (selNode != null) { 
          DefaultMutableTreeNode newNode = new DefaultMutableTreeNode("New Node" + (selNode.getChildCount() + 1)); 
          model.insertNodeInto(newNode, selNode, selNode.getChildCount());// 
          //model.insertNodeInto(newNode, (MutableTreeNode) root, root.getChildCount()); 
          tree.expandPath(new TreePath(model.getPathToRoot(newNode.getParent()))); 

          TreeNode[] nodes = model.getPathToRoot(newNode); 
          TreePath path = new TreePath(nodes); 

          System.out.println(selNode.getChildCount() + ", " + path); 
          tree.scrollPathToVisible(path); 
          tree.setSelectionPath(path); 
          tree.startEditingAtPath(path); 
          model.reload((TreeNode) model.getRoot()); 
         }else{ 
          System.out.println("selNode is null"); 
         } 
        } 
       }); 
       //addHoverAction(item);// 
       //item.add(new JMenuItem("page")); 

       popup.add(item); 
       popup.add(new JMenuItem("Move: ")); 
       popup.add(sectionsMenu); 
       popup.show(tree, x, y); 
      } 

      public void mousePressed(MouseEvent e) { 
       if (e.isPopupTrigger()) { 
        myPopupEvent(e); 
       } 
      } 

      public void mouseReleased(MouseEvent e) { 
       if (e.isPopupTrigger()) { 
        myPopupEvent(e); 
       } 
      } 
     }; 
     tree.addMouseListener(ma); 
    } 

    public static void addHoverAction(final JComponent com) { 

     MouseAdapter adapter = new MouseAdapter() { 
      public void mouseEntered(java.awt.event.MouseEvent evt) { 
       System.out.println("mouse Entered: "); 
       if (true) { 
        JPopupMenu pop = new JPopupMenu(); 
        pop.add(new JMenuItem("Node")); 

        pop.show(com.getParent(), com.getWidth(), com.getHeight()); 
       } 
      } 
     }; 
     com.addMouseListener(adapter); 
    } 
} 

感謝您提前解決問題和描述問題。下面線

DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); 

代替

DefaultTreeModel model = new DefaultTreeModel(root); 

回答