Right-click context menu for Java JTree?

后端 未结 6 1976
走了就别回头了
走了就别回头了 2020-12-24 12:07

I\'m trying to implement pop-up menus in Java JTree. I\'ve sub-classed DefaultTreeCellRenderer (to change node appearance) and DefaultTreeCellEditor (to create Components to

6条回答
  •  清歌不尽
    2020-12-24 12:45

    Taken right out of the JTree API

     // If you are interested in detecting either double-click events or when a user clicks on a node, regardless of whether or not it was selected, we recommend you do the following:
    
     final JTree tree = ...;
    
     MouseListener ml = new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
             int selRow = tree.getRowForLocation(e.getX(), e.getY());
             TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
             if(selRow != -1) {
                 if(e.getClickCount() == 1) {
                     mySingleClick(selRow, selPath);
                 }
                 else if(e.getClickCount() == 2) {
                     myDoubleClick(selRow, selPath);
                 }
             }
         }
     };
     tree.addMouseListener(ml);
    

    Of course you need to modify it a bit for right click instead of left click

提交回复
热议问题