Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2001-07-03
4
1,742 Views
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.
0
Comment
Question by:huer
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 200 total points
ID: 6251485
import java.awt.*;
import java.awt.event.*;
import java.io.*;
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() {
    init();
  }

  private void init() {
    setSize(200, 400);
    setLocation(200, 100);
    tree = new JTree();
    tree.addMouseListener(new TreeMouseAdapter());
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setShowsRootHandles(true);
    tree.setEditable(true);
    tree.putClientProperty("JTree.lineStyle", "Angled");
    popup = new TreePopup();
    getContentPane().add(new JScrollPane(tree));
    setVisible(true);
  }

  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) {
      item.addActionListener(this);
      add(item);
    }

    public void actionPerformed(ActionEvent ae) {
      String action = ae.getActionCommand();
      if(action.startsWith("Rename"))
        rename();
    }
  }

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

    public void mouseReleased(MouseEvent me) {
      checkPopup(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;
      popup.show(tree, x1, y1);
    }
  }

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

Expert Comment

by:Ovi
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).
0
 

Author Comment

by:huer
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!
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6254326
Any time.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Groovy problem when using SOAPUI : DispatchException occurred 7 59
mysql jsp example issue 32 49
java example issue 5 25
Eclipse for Java EE development 2 28
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

828 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