how to rename a node in JTree when user clicks on the pop-up menu associated with the tree

Posted on 2001-07-03
Last Modified: 2011-10-03
 I need to rename a node in a JTree. I can do it by setting JTree's setEditable to be true, but  that is not what i want.
  What i really want is that the users can right click on the pop-up menu "Rename" and then change the node name, just exactly like what the the window explorer does.
  Please help me.
Question by:huer
  • 3

Accepted Solution

Ovi earned 200 total points
ID: 6251485
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;

public class EditableTree extends JFrame {
  private JTree tree;
  private DefaultTreeModel model;
  private TreePopup popup;
  private int x = 0, y = 0;

  public EditableTree() {

  private void init() {
    setSize(200, 400);
    setLocation(200, 100);
    tree = new JTree();
    tree.addMouseListener(new TreeMouseAdapter());
    tree.putClientProperty("JTree.lineStyle", "Angled");
    popup = new TreePopup();
    getContentPane().add(new JScrollPane(tree));

  public void rename() {
    tree.startEditingAtPath(tree.getClosestPathForLocation(x, y));

  class TreePopup extends JPopupMenu implements ActionListener {
    public TreePopup() {
      addItem(new JMenuItem("Rename"));

    public void addItem(JMenuItem item) {

    public void actionPerformed(ActionEvent ae) {
      String action = ae.getActionCommand();

  class TreeMouseAdapter extends MouseAdapter {
    public void mousePressed(MouseEvent me) {

    public void mouseReleased(MouseEvent me) {
    public void checkPopup(MouseEvent me) {
      if(me.isPopupTrigger()) {
        int x = me.getX(); int y = me.getY(); rightPressed(x, y);

    public void rightPressed(int x1, int y1) {
      tree.setSelectionPath(tree.getClosestPathForLocation(x1, y1));
      x = x1;y = y1;, x1, y1);

  public static void main(String[] args) {
    EditableTree editableTree1 = new EditableTree();

Expert Comment

ID: 6251502
This is the exact behaviuor as in WindowsExplorer. The tree must be made editable, even if you want to programically edit an node. All you have to do is to right click a node and push the "Rename" popup item. If you want that a node to be selected only at a left mouse click, please comment the line : tree.setSelectionPath(tree.getClosestPathForLocation(x1, y1)); from the method TreeMouseAdapter.rightPressed(int, int) (in this moment if you do a right or left click the node becomes selected anyway - WindowsExplorer style).

Author Comment

ID: 6253581
Thank you so much Qvi. You are really an expert in Java.
I tried the code you sent to me. It is just perfect.
Thank you!

Expert Comment

ID: 6254326
Any time.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 18
Java JRE greater than 1.6 5 49
tomcat not starting 6 23
expectj telnet failing 5 16
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now