- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement the mouse right-click on each node of JTree in Java?n
A JTree is a subclass of JComponent class that can be used to display the data with the hierarchical properties by adding nodes to nodes and keeps the concept of parent and child node. Each element in the tree becomes a node. The nodes are expandable and collapsible. We can implement the mouse right-click on each node of a JTree using the mouseReleased() method of MouseAdapter class and need to call show() method of JPopupMenu class to show the popup menu on the tree node.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.tree.*; public class JTreeRightClickTest extends JFrame { public JTreeRightClickTest() { DefaultMutableTreeNode root = createNodes(); JTree tree = new JTree(root); final TreePopup treePopup = new TreePopup(tree); tree.addMouseListener(new MouseAdapter() { public void mouseReleased(MouseEvent e) { if(e.isPopupTrigger()) { treePopup.show(e.getComponent(), e.getX(), e.getY()); } } }); add(new JScrollPane(tree), BorderLayout.NORTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setLocationRelativeTo(null); setVisible(true); } public static DefaultMutableTreeNode createNodes() { DefaultMutableTreeNode root = new DefaultMutableTreeNode("Technology"); DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Java"); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Python"); DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("Selenium"); node1.add(new DefaultMutableTreeNode("Programming Language")); node2.add(new DefaultMutableTreeNode("Programming Language")); node3.add(new DefaultMutableTreeNode("Testing Framework")); root.add(node1); root.add(node2); root.add(node3); return root; } public static void main(String args[]) { new JTreeRightClickTest(); } } class TreePopup extends JPopupMenu { public TreePopup(JTree tree) { JMenuItem delete = new JMenuItem("Delete"); JMenuItem add = new JMenuItem("Add"); delete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.out.println("Delete child"); } }); add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.out.println("Add child"); } }); add(delete); add(new JSeparator()); add(add); } }
Output
- Related Articles
- How to implement the mouse right-click on each node of JTree in Java?\n
- How to handle a mouse right click event using jQuery?
- How can we implement right click menu using JPopupMenu in Java?
- How to get the depth of root node in a JTree with Java?
- How to find a node in a JTree Component with Java?
- Java Program to count the child of root node in a JTree
- How to implement click event on toolbar icon?
- Java Program to get the previous node from a JTree
- I want to return the next node after this node in a JTree with Java
- Get the number of siblings of a node in JTree with Java
- Java Program to get the count of child nodes of any node in JTree
- How to get the leaf after this node in a JTree component with Java?
- Get the number of levels above a node in JTree with Java
- Trigger an event IMMEDIATELY on mouse click, not after I let go of the mouse - JavaScript?
- How to get the number of siblings of this node in a JTree?
- Matplotlib – How to show the coordinates of a point upon mouse click?

Advertisements