Getting at a JTree's mouse listener

J

John

I am using context menus (JPopupMenu) with a JTree and have managed to
get to the stage where my context menus appear as required after a right
click. I would like the item to which the context menu applies to be in
focus after the right click (it doesn't do this by default). I can get
the effect I want by left clicking the item then right clicking it, so I
thought I would modify the actionlistener so that when a right click is
received, a left click is fired before the menu is displayed. For some
reason I can't seem to find the ActionListener that deals with the
mouseevents on the JTree? Any ideas?

John
 
S

SPG

At the point you get the right click event to show the popup, you know (or
can work out) the node the mouse was over.

Why do you not simple use the setSelectionPath() method on the tree before
showing the popup?

Code for your mousePressed event:

public void mousePressed(MouseEvent e) {
//Discover the row that was selected (IE: The branch)
int selRow = tree.getRowForLocation(e.getX(), e.getY());
//Get the selection path for the row
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
if( e.isPopUpTrigger()){
//Make the selection
tree.setSelectionPath(selPath);
//Now do pop up
showPopup();
}
}
}
}

Steve
 
J

John

SPG wrote:

At the point you get the right click event to show the popup, you know (or
can work out) the node the mouse was over.

Why do you not simple use the setSelectionPath() method on the tree before
showing the popup?

Code for your mousePressed event:

public void mousePressed(MouseEvent e) {
//Discover the row that was selected (IE: The branch)
int selRow = tree.getRowForLocation(e.getX(), e.getY());
//Get the selection path for the row
TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
if(selRow != -1) {
if(e.getClickCount() == 1) {
if( e.isPopUpTrigger()){
//Make the selection
tree.setSelectionPath(selPath);
//Now do pop up
showPopup();
}
}
}
}

Steve

Have you got this working? I tried the same thing before I posted and
found that the the item went back to the nonfocused state as soon as the
popup displayed (after about 100ms). If you have, I'll have to check the
code of the pop-up.

John
 
S

SPG

Hi John,

Seems OK here. May be something to do with Parent components etc.
Try adding a PopupMenuListener to the popup menu, then in the cancel or
becomeInvisible events do the reselection there - Just pass the treeapth to
your listener..

Alternatively, pass the treepath to the action object you create when you
load the popup and do the re-selction when the action completes.

It may have something to do with the tree itself losing focus..

Steve
 
S

SPG

Alternatively, have you tried getting the last path component and setting
that as selected via th tree before showing the popup?

One last try, which is not int he source I gave you but in my samples.. call
super.mousePressed(e) after you have called the popup show...


Steve
 
J

John

Cheers for the responses Steve. I suspect that the problem stems from
the fact that I am controlling the event dispatch thread very closely
(to allow using JInternalFrames like arbitrarily nested modal dialogs).
I tried your suggestions and then decided to do a quick-and-dirty.
This consists of getting the popup menu to set a boolean flag in the
TreeCellRenderer which makes the displayed component act like it has
focus when it doesn't. All I have to do is make sure that the popup menu
is not disposed of without having reset the flag.

John

(sorry about top post, when in Rome and all that!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top