• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

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
selected.

Any help will be welcome.

Thanks in advance
0
ugarg
Asked:
ugarg
  • 2
1 Solution
 
OviCommented:
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() {
          init();
     }
     
     private void init() {
          getContentPane().setLayout(new BorderLayout());
          setSize(400, 300);
          setLocation(300, 100);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    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()) {
         popup.show(tree, me.getX(), me.getY());
        }
      }
    });
    setVisible(true);
  }
 
     public static void main(String[] args) {
          PopupKey t = new PopupKey();    
     }
     
}
0
 
tomboshellCommented:
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.

Tom
0
 
tomboshellCommented:
I guess that my comment helped you.

Thanks for the grade!
Tom
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now