Solved

Keyboard up/down arrow does not work for popup menus

Posted on 2002-04-17
3
174 Views
Last Modified: 2010-03-31
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
Comment
Question by:ugarg
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 6949686
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
 
LVL 7

Accepted Solution

by:
tomboshell earned 75 total points
ID: 6949817
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
 
LVL 7

Expert Comment

by:tomboshell
ID: 6953581
I guess that my comment helped you.

Thanks for the grade!
Tom
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
Configure a Bean in an XML file 4 42
maven disable workspace resolution 1 22
ejb entity bean example 2 17
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

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