Keyboard up/down arrow does not work for popup menus

I am having a JTree. On the right click of each node i am displaying a popup menu.
PopUp menu is getting displayed when i right click, but than if I press the up or down
arrow, the options in popup menu is not getting selected instead the treenode selection
is getting changed while popup menu still remains on the scree.

I want that on pressing up or down key, popup menu options should get selected if popup
menu is visible, else if popupmenu is not visible than the respective treenode should get

Any help will be welcome.

Thanks in advance
Who is Participating?
tomboshellConnect With a Mentor Commented:
It sounds like the Tree had still maintained focus.  My guess is that you had activated the pop-up, it is not modular, and no focus-grabbing routines were activated.  Try grabbing or setting the focus when you activate/show the pop-up.  Then make sure that you return the focus to the tree when leaving the pop-up.

Here is working fine. Post your code or a sample code like this to give me a hint.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PopupKey extends JFrame {
  private JTree tree;
  private JPopupMenu popup;
     public PopupKey() {
     private void init() {
          getContentPane().setLayout(new BorderLayout());
          setSize(400, 300);
          setLocation(300, 100);
    tree = new JTree();
    popup = new JPopupMenu("Demo");
    popup.add(new JMenuItem("Action 1"));
    popup.add(new JMenuItem("Action 2"));
    popup.add(new JMenuItem("Action 3"));
    popup.add(new JMenuItem("Action 4"));
    popup.add(new JMenuItem("Action 5"));
    popup.add(new JMenuItem("Action 6"));
    popup.add(new JMenuItem("Action 7"));
    getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
    tree.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent me) { checkPopup(me); }
      public void mouseReleased(MouseEvent me) { checkPopup(me); }
      protected void checkPopup(MouseEvent me) {
        if(me.isPopupTrigger()) {, me.getX(), me.getY());
     public static void main(String[] args) {
          PopupKey t = new PopupKey();    
I guess that my comment helped you.

Thanks for the grade!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.