Add JPopup menu by right clicking on node in Swing in Java

In GUI,I am displaying one JTree at the left hand side of JPanel. Now for each Node(leaf), on Mouse right click I want to display JPopup menu asking for displaying the statistics about Node in right JPanel.

As i am new to swing,Could any one help with code. Thanks in Advance.

Regards, Tushar Dodia.

Answers


Use JTree's method

public TreePath getPathForLocation(int x, int y)

Then TreePath

public Object getLastPathComponent()

That returns you desired node from point where user right clicked.


Seem to have caused a bit of confusion (confusing myself ;-) - so here's a code snippet for doing target location related configuration of the componentPopup

    JPopupMenu popup = new JPopupMenu();
    final Action action = new AbstractAction("empty") {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
        }
    };
    popup.add(action); 
    JTree tree = new JTree() {

        /** 
         * @inherited <p>
         */
        @Override
        public Point getPopupLocation(MouseEvent e) {
            if (e != null) {
               TreePath path = getClosestPathForLocation(e.getX(), e.getY());
               action.putValue(Action.NAME, String.valueOf(path.getLastPathComponent()));
               return e.getPoint();
            }
            action.putValue(Action.NAME, "no mouse"); 
            return null;
        }

    };
    tree.setComponentPopupMenu(popup);

I took @kleopatra solution and changed it slightly. Maybe it isn't the best way but works for me.

JTree tree = new JTree() {
    private static final long serialVersionUID = 1L;
    @Override public Point getPopupLocation(MouseEvent e) {
        if (e == null) return new Point(0,0);
        TreePath path = getClosestPathForLocation(e.getX(), e.getY());
        Object selected = path != null ? path.getLastPathComponent() : null;
        setComponentPopupMenu(getMenuForTreeNode(getComponentPopupMenu(), selected));
        setSelectionPath(path);
        return e.getPoint();
    }
};



public JPopupMenu getMenuForTreeNode(JPopupMenu menu, Object treeNode) {
    if (menu == null) menu = new JPopupMenu("Menu:");
    menu.removeAll();
    if (treeNode instanceof MyTreeItem) {
        menu.add(new JMenuItem("This is my tree item: " + treeNode.toString()));
    }
    return menu;
}

Need Your Help

UpdatePanel and JQuery UI/Tabs selected tab

asp.net jquery-ui updatepanel

I have a server side method that can take long time. I decided to display a loading process modal and for that I used this tutorial. It works fine, but my problem is the following.

Java bytecode error using AspectJ

java aspectj bytecode aop

I am trying to modify the working of some code using AspectJ. The aspectJ code is of the form (Profiler.java):

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.