Solved

Keyboard up/down arrow does not work for popup menus

Posted on 2002-04-17
3
159 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now